Wednesday 7 September 2016

Solar system using structure

  Develop a structure to represent a planets in the solar system. Each planet has the field for the planets name, its distance from the sun in miles and the number of moon it has. Write a program to read the data for each planet and store. Also print the name of the planet that has the highest number of moons.

#include<stdio.h>
struct SolarSystem
{
            char name[30];
            int distance;
            int moons;
}planet[9];

void main()
{
                 int i, max_moons,  pos;
                printf("\nEnter planets records: \n");
                for(i=0;i<9;i++)
                {
                                printf("\nEnter the name of the planet: ");
                                scanf("%s",  planet[i].name);
                                printf("\nEnter the distance of the planet from sun: ");
                                scanf("%d", &planet[i].distance);
                                printf("\nEnter the number of moons for planet: ");
                                scanf("%d", &planet[i].moons);
                }
                printf("\nPlanets records: \n");
                printf("\n---------------------------------------");
                printf("\nName\tDistance\tMoon");
                printf("\n---------------------------------------");
                for(i=0;i<9;i++)
                {
                                printf("\n%s\t%d\t%d", planet[i].name,planet[i].distance, planet[i].moons);
                }
                max_moons = planet[0].moons;
                pos=0;
                for(i=0;i<9;i++)
                {
                                if(planet[i].moons> max_moons)
                                {
                                                max_moons = planet[i].moons;
                                                pos=i;
                                }
                }
                printf("\nThe planet having highest number of moons is %s",  planet[pos].name);
}

Output:
(Note:  Distance in million miles)
Enter planets records:
Enter the name of the planet: Mercury
Enter the distance of the planet from sun: 36
Enter the number of moons for planet: 0

Enter the name of the planet: Venus
Enter the distance of the planet from sun: 67
Enter the number of moons for planet: 0

Enter the name of the planet: Earth
Enter the distance of the planet from sun: 93
Enter the number of moons for planet: 1

Enter the name of the planet: Mars
Enter the distance of the planet from sun: 142
Enter the number of moons for planet: 2

Enter the name of the planet: Jupiter
Enter the distance of the planet from sun: 483
Enter the number of moons for planet: 62
Enter the name of the planet: Saturn
Enter the distance of the planet from sun: 888
Enter the number of moons for planet: 53

Enter the name of the planet: Uranus
Enter the distance of the planet from sun: 1784
Enter the number of moons for planet: 21

Enter the name of the planet: Neptune
Enter the distance of the planet from sun: 2794
Enter the number of moons for planet: 13

Enter the name of the planet: Pluto
Enter the distance of the planet from sun: 2647
Enter the number of moons for planet: 3

Planets records:
----------------------------------------------------------------------
Name                    Distance               Moon
-----------------------------------------------------------------------
Mercury                 36                          0
Venus                     67                          0
Earth                      93                          1
Mars                      142                         2
Jupiter                   483                         62
Saturn                   888                         53
Uranus                 1784                        21
Neptune               2794                        13
Pluto                    2647                         3
The planet having highest number of moons is Jupiter


No comments:

Post a Comment