Structure Program in C

1. Program to create a structure named World having members name,address,city,street,telephone,age as members.Input the record for one person and display the records.

 #include<stdio.h>
 #include<conio.h>
 struct World
         {
         char name[50];
         char address[50];
         char city[50];
         int street;
         int telephone;
         int age;
         };
         void main()
         {
         struct World W;
         printf("Enter the name\n");
         gets(W.name);
         printf("Enter address\n");
         gets(W.address);
         printf("Enter city\n");
         gets(W.city);
         printf("Enter the street\n");
         scanf("%d",&W.street);
         printf("Enter the telephone number\n");
         scanf("%d",&W.telephone);
         printf("Enter the age\n");
         scanf("%d",&W.age);

         printf("The information are\n");
         printf("Name=%s\n Address=%s\n City=%s\n Street=%d\n Telephone no=%d\n Age=%d\n",W.name,W.address,W.city,W.street,W.telephone,W.age);
         getch();
         }
Output: 
                                                                                                                                                                                                                               














2.. Program to create a structure named Student having members name,country,city,telephone,age,id as members.Input the record for n student  and display the records.

 #include<stdio.h>
 #include<conio.h>
 struct Student
         {
         char name[50];
         char country[50];
         char city[50];
         int telephone;
         int age;
         int ID;
         };
         void main()
         {
         struct Student S[100];
         int i,n;
         printf("Enter the number of students\n");
         scanf("%d",&n);
         for(i=0;i<n;i++)
         {
         printf("Enter the information of student\n");
         printf("Enter the name\n");
         scanf("%s",S[i].name);
         printf("Enter Country\n");
         scanf("%s",S[i].country);
         printf("Enter city\n");
         scanf("%s",S[i].city);
         printf("Enter the ID\n");
         scanf("%d",&S[i].ID);
         printf("Enter the telephone number\n");
         scanf("%d",&S[i].telephone);
         printf("Enter the age\n");
         scanf("%d",&S[i].age);
                       }
         printf("The information are\n");
         printf("Name\t Country\t City\t ID\t Telephone no\t Age\n");

         for(i=0;i<n;i++)
         {

         printf("%s\t\t%s\t\t%s\t\t%d\t\t%d\t\t%d\t\t",S[i].name,S[i].country,S[i].city,S[i].ID,S[i].telephone,S[i].age);
         }
         getch();
         }

Output:


















2.. Program to create a structure named Student having members name,country,age,id as members.Input the record for n student  and display the records when  the country match with America.


 #include<string.h>
 #include<conio.h>
 #include<stdio.h>
 struct Student
         {
         char name[50];
         char country[50];
         char city[50];
         int age;
         int ID;
         };
         int main()
         {
         struct Student S[100];
         int i,n;
         printf("Enter the number of students\n");
         scanf("%d",&n);
         for(i=0;i<n;i++)
         {
       
  printf("Enter the information of student\n");
         printf("Enter the name\n");

         scanf("%s",S[i].name);

         printf("Enter Country\n");

         scanf("%s",S[i].country);

         printf("Enter city\n");

         scanf("%s",S[i].city);

         printf("Enter the ID\n");

         scanf("%d",&S[i].ID);

         printf("Enter the age\n");

         scanf("%d",&S[i].age);

                       }

         printf("The information are\n");

         printf("Name\t Country\t City\t ID\t Age\n");

         for(i=0;i<n;i++)
         {
         if((strcmp(S[i].country,"America"))==0)
         {

         printf("%s\t\t%s\t\t%s\t\t%d\t\t%d\t\t",S[i].name,S[i].country,S[i].city,S[i].ID,S[i].age);
         }
         }
         getch();
         }

            Output



































Here the country given as America is result out but the country given Norway is not resulted as output as we have compared country with America so when the country America is given as input,the  output will come.

4... Program to create a structure named Student having members name,country,age,id,date of birth  as members.Input the record for n student  and display the records when  the city match with cool.

 #include<string.h>
 #include<conio.h>
 #include<stdio.h>
 struct DOB
 {
        int yy;
        int mm;
       int  dd;
 };
 

 struct Student
         {
         char name[50];
         char country[50];
         char city[50];
         int age;
         int ID;
         struct DOB date;
         };
         
         int main()
         {
         struct Student S[100];

         int i,n;
         printf("Enter the number of students\n");
         scanf("%d",&n);
         for(i=0;i<n;i++)
         {
       
  printf("Enter the information of student\n");

         printf("Enter the name\n");

         scanf("%s",S[i].name);

         printf("Enter Country\n");

         scanf("%s",S[i].country);

         printf("Enter city\n");

         scanf("%s",S[i].city);

         printf("Enter the ID\n");

         scanf("%d",&S[i].ID);

         printf("Enter the age\n");

         scanf("%d",&S[i].age);

         printf("Enter the date of birth in yymmdd\n");

         scanf("%d %d %d",&S[i].date.yy,&S[i].date.mm,&S[i].date.dd);

                       }
         printf("The information are\n");

         printf("Name\t Country\t City\t ID\t Age\n");

         for(i=0;i<n;i++)
         {
         if((strcmp(S[i].City,"cool"))==0)
         {

         printf("%s\t\t%s\t\t%s\t\t%d\t\t%d\t\t",S[i].name,S[i].country,S[i].city,S[i].ID,S[i].age);
         printf("Date of birth is %d %d  %d",S[i].date.yy,S[i].date.mm,S[i].date.yy);
         }
         }
         getch();
         }

Output:



































5. Program to create a structure named  Student having members name,address,city,roll no,age as members.Input the record for one person and display the records using pointer.

 #include<stdio.h>
 #include<conio.h>
 struct student

         {

         char name[50];

         char address[50];

         char city[50];
     int roll;
         int age;

         };

         int main()

         {

         struct student s;
         struct student *ptr;
         ptr=&s;

         printf("Enter the name\n");

         gets(ptr->name);

         printf("Enter address\n");

         gets(ptr->address);

         printf("Enter city\n");

       gets(ptr->city);
       
printf("Enter the roll no\n");

scanf("%d",&(ptr->roll));
         printf("Enter the age\n");

         scanf("%d",&(ptr->age));



         printf("The information are\n");

         printf("Name=%s\n Address=%s\n City=%s\nRoll%d\n Age=%d\n",ptr->name,ptr->address,ptr->city,ptr->roll,ptr->age);

         getch();

         }
Output:


























6. Program to create a structure named  Student having members name,address,city,roll no,age as members.Input the record for "n" person and display the records using pointer.



 #include<stdio.h>

 #include<conio.h>

 struct student

         {

         char name[50];

         char address[50];

         char city[50];
     int roll;
         int age;

         };

         int main()

         {
int i,n;
         struct student s[100];
         struct student *ptr;
         ptr=s;
         for(i=0;i<n;i++)
         {
        

         printf("Enter the name\n");

         scanf("%s",(ptr+i)->name);

         printf("Enter address\n");

         scanf("%s",(ptr+i)->address);

         printf("Enter city\n");

       scanf("%s",(ptr+i)->city);
       
printf("Enter the roll no\n");

scanf("%d",&((ptr+i)->roll));
         printf("Enter the age\n");

         scanf("%d",&(ptr+i)->age);
}
for(i=0;i<n;i++)
{
         printf("The information are\n");
      printf("Name=%s\n Address=%s\n City=%s\nRoll%d\n Age=%d\n",(ptr+i)->name,(ptr+i)->address,(ptr+i)->city,(ptr+i)->roll,(ptr+i)->age);
       }
         getch();

         }
Output:

.



 

Comments