How to Draw Solar System using C Language with Source Code

solar system using C or C++ programming language

Introduction :

The Solar System has captivated the human imagination for centuries because of its magnificent display of planets, moons and other celestial objects. Using the C or C++ programming language, you can create a visual representation of the Solar System with the sun and its planets with their relative positions.

In this article, I shall explore you how to draw the solar system using the C or C++ programming language with source code. Here, I create a program using basic shapes and colors of graphics.h library in the Turbo C++ IDE that allows us to draw the solar system. With the help of graphics.h library in the Turbo C++ IDE, you can easily render a simple graphical representation of the solar system on the screen and bring our solar system to life.

About the solar system :

The study of the solar system is known as astronomy. The solar system is the planetary system which is a collection of celestial bodies. The celestial bodies are gravitationally bound to the Sun and they orbit around the Sun continuously. It is located in the Milky Way galaxy. There are the Sun, eight planets (Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune), their moons, asteroids, comets and other smaller objects in the solar system.

The Sun is the heart of the solar system. The Sun is situated at the center of the solar system. It holds about 99.86% of the total mass of the solar system. It is primarily composed of hydrogen and helium. The Sun generates energy through nuclear fusion and gives heat and light energy to the planets. All the planets orbit the Sun in elliptical paths.

In order to distance from the Sun, there are eight planets in the solar system. Mercury is the closest planet to the Sun. It is the smallest planet in the solar system (because Pluto is dwarf planet). Venus is known as earth’s “evil twin”. Venus is the hottest and brightest planet in the solar system. It is the closest planet to the earth and similar in size to earth.

Earth is our home planet and it is the only planet where we found living animals and trees. Here we also found diverse ecosystems and liquid water. It is the third planet from the Sun. It is known as “blue planet” in the solar system. Mars is the fourth planet from the Sun. Mars is known as the “Red Planet” due to its reddish appearance. There are polar ice caps in Mars. Olympus Mons is a volcano located on Mars. It is the largest volcano in the solar system.

Jupiter is the largest planet in the solar system. There is the Great Red Spot in the Jupiter. It is also called “giant planet”. Ganymede of Jupiter is the largest moon in the solar system. Saturn is famous for its magnificent and prominent ring system. Saturn has numerous moons and titan is the largest among them. Uranus is unique among the all planets in the solar system because it rotates on its side. Neptune is the farthest planet from the Sun.

Note that, nowadays Pluto is considered as a dwarf planet.

About the program :

In this program, you see the solar system where all the planets moving around the sun in their own orbits. Here, you can also see the position, color and the name of the planets will be displayed on the screen. The program runs continuously until the user presses any key.

Explanation of the program :

In the program, you have to first include the necessary libraries such as graphics.h for use graphical function, conio.h for use kbhit() function, dos.h for delay() function and math.h for sin() and cos() functions. Now using the initgraph() function, initialize the graphics mode with specify the graphics driver and graphics mode. You have to show the BGI path in initgraph() function.

The getmaxx() and getmaxy() functions use for get the x and y coordinate of the graphics window. After that, in the while loop write the name of the program and my name using settextstyle() and outtextxy() functions. Next, setbkcolor() function use for set the black background color of the graphics window. Then I draw the sun at the center by fillellipse() function and set the color and style by setfillstyle() function.

At the same time draw all the planets with their relative position and different colors using setcolor(), setfillstyle() and fillellipse() functions to make them visually distinguishable. Display the name of planets by settextstyle() and outtextxy() functions. Also, draw the orbits of all the planets using ellipse() function. To calculate the changing position of planets, I use sin() and cos() functions. The kbhit() function use for check that the user enters any key or not.

To make the solar system come to life, you have to animate the program by updating the positions of the planets and redrawing them in the next frame. You can use the delay() function to control the animation speed or the movement of the planets. You have to refresh the screen continuously using the cleardevice() function. Once your program has finished, use the closegraph() function to close the graphics mode.

How run the program :

To get started, you need the Turbo C++ IDE installed on your computer. Turbo C++ IDE is a compiler and an integrated development environment specifically designed for the C or C++ programming language. By default, the graphics.h library is included in the Turbo C++ IDE. After open the Turbo C++ IDE, create a C or C++ file with .c or .cpp extension.

Now copy the below source code of a solar system program by C or C++ programming language and paste in your C or C++ file of the Turbo C++ IDE. If you do not know how to copy paste in the Turbo C++ IDE, just follow this link. You can also see how to use graphics.h in Turbo C++ IDE from here.

Source code of solar system by C or C++ graphics programming language :

Just copy the below code of drawing solar system using C or C++ graphics programming language and use in your program.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
int main(void)
{
    int x, y, i, a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, j, m = 0;
    int graphic_driver = DETECT, graphic_mode;
    initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
    x = getmaxx() / 2;
    y = getmaxy() / 2;
    while (!kbhit())
    {
        settextstyle(1, 0, 4);
        outtextxy(0, 0, "solar");
        outtextxy(0, 30, "system");
        outtextxy(530, 0, "puskar");
        outtextxy(530, 30, "jasu");
        setbkcolor(0);
        setcolor(4);
        setfillstyle(1, 4);
        fillellipse(x, y, 30, 30);
        i = 60 * cos((0 + a) * 3.14159 / 180);
        j = 50 * sin((0 + a) * 3.14159 / 180);
        a += 30;
        if (a > 360)
            a = 0;
        setcolor(8);
        setfillstyle(1, 8);
        fillellipse(x + i, y - j, 10, 10);
        settextstyle(2, 0, 6);
        outtextxy(x + i, y - j, "murcury");
        ellipse(x, y, 0, 360, 60, 50);
        i = 90 * cos((90 + b) * 3.14159 / 180);
        j = 70 * sin((90 + b) * 3.14159 / 180);
        b += 25;
        if (b > 360)
            b = 0;
        setcolor(14);
        setfillstyle(1, 14);
        fillellipse(x + i, y - j, 10, 10);
        outtextxy(x + i, y - j, "venus");
        ellipse(x, y, 0, 360, 90, 70);
        i = 120 * cos((180 + c) * 3.14159 / 180);
        j = 100 * sin((180 + c) * 3.14159 / 180);
        c += 20;
        if (c > 360)
            c = 0;
        setcolor(1);
        setfillstyle(1, 1);
        fillellipse(x + i, y - j, 10, 10);
        outtextxy(x + i, y - j, "earth");
        ellipse(x, y, 0, 360, 120, 100);
        i = 150 * cos((60 + d) * 3.14159 / 180);
        j = 120 * sin((60 + d) * 3.14159 / 180);
        d += 15;
        if (d > 360)
            d = 0;
        setcolor(4);
        setfillstyle(1, 4);
        fillellipse(x + i, y - j, 10, 10);
        outtextxy(x + i, y - j, "mars");
        ellipse(x, y, 0, 360, 150, 120);
        i = 180 * cos((0 + e) * 3.14159 / 180);
        j = 150 * sin((0 + e) * 3.14159 / 180);
        e += 10;
        if (e > 360)
            e = 0;
        setcolor(12);
        setfillstyle(1, 12);
        fillellipse(x + i, y - j, 10, 10);
        outtextxy(x + i, y - j, "jupiter");
        ellipse(x, y, 0, 360, 180, 150);
        i = 210 * cos((240 + f) * 3.14159 / 180);
        j = 170 * sin((240 + f) * 3.14159 / 180);
        f += 5;
        if (f > 360)
            f = 0;
        setcolor(7);
        setfillstyle(1, 7);
        fillellipse(x + i, y - j, 10, 10);
        outtextxy(x + i, y - j, "saturn");
        ellipse(x, y, 0, 360, 210, 170);
        i = 240 * cos((90 + g) * 3.14159 / 180);
        j = 190 * sin((90 + g) * 3.14159 / 180);
        g += 3;
        if (g > 360)
            g = 0;
        setcolor(9);
        setfillstyle(1, 9);
        fillellipse(x + i, y - j, 10, 10);
        outtextxy(x + i, y - j, "uranus");
        ellipse(x, y, 0, 360, 240, 190);
        i = 270 * cos((270 + h) * 3.14159 / 180);
        j = 210 * sin((270 + h) * 3.14159 / 180);
        h += 2;
        if (h > 360)
            h = 0;
        setcolor(9);
        setfillstyle(1, 9);
        fillellipse(x + i, y - j, 10, 10);
        outtextxy(x + i, y - j, "neptune");
        ellipse(x, y, 0, 360, 270, 210);
        i = 300 * cos((330 + g) * 3.14159 / 180);
        j = 230 * sin((330 + g) * 3.14159 / 180);
        m++;
        if (m > 360)
            m = 0;
        setcolor(6);
        setfillstyle(1, 6);
        fillellipse(x + i, y - j, 10, 10);
        outtextxy(x + i, y - j, "pluto");
        ellipse(x, y, 0, 360, 300, 230);
        delay(100);
        cleardevice();
    }
    closegraph();
    return 0;
}

Output :

After compile the program successfully, run the program to see the solar system come to life on your screen. You can see the output in my YouTube channel. In my above program, I add some code for the display orbit of planets so when you run the above code on your pc you see some change than the output on my YouTube video.

Conclusion :

By following the above program, you have learned how to draw the solar system using the graphics.h library in Turbo C++ IDE by C or C++ programming language. By utilizing the C or C++ programming language, you can create a graphical representation of the solar system with the Sun and its planets move around the Sun. Thank you for visiting my site.

Scroll to Top