Programs Using Structure


Declare a structure named “library” which will contains data members: bookno, bookname, price, publication. Read and print the structure.
#include<stdio.h>
#include<conio.h>
struct library 
{
     int bookno;
     char bookname[20];
     float price;
     char publication[20];
};
void main()
{
     struct library l1;
     clrscr();
     printf(“Enter Book Number:”);
     scanf(“%d”,&l1.bookno);
     printf(“Enter Book Name:”);
     scanf(“%s”,l1.bookname);
     printf(“Enter Book Price:”);
     scanf(“%f”,&l1.price);
     printf(“Enter Publication:”);
     scanf(“%s”,l1.publication);
     printf(“Book Number:%d\n”,l1.bookno);
     printf(“Book Name:%s\n”,l1.bookname);
     printf(“Book Price:%f\n”,l1.price);
     printf(“Publication:%s\n”,l1.publication);
    getch();
}

Output

Enter Book Number:001
Enter Book Name: Programming In C
Enter Book Price: 250.00
Enter Publication: XYZ

Book Number:001
Book Name: Programming In C
Book Price: 250.00
Publication: XYZ
     
Create a structure named student that accepts student rollno, name, gender and marks of 3 subjects. Find total of marks and percentage. Create an array of structure variable to enter information for three students.
#include<stdio.h>
#include<conio.h>
struct student
{
     char name[20];
     int regno;
     char gender[10];
     int mark1,mark2,mark3,total;
     float per;
};
void main()
{
     struct student s[3];
     int i;
     clrscr();
     for(i=0;i<3;i++)
     {
          printf("Enter Roll Number:");
          scanf("%d",&s[i].regno);
          printf("Enter Name:");
          scanf("%s",s[i].name);
          printf("Enter gender:");
          scanf("%s",s[i].gender);
          printf("Enter mark1:");
          scanf("%d",&s[i].mark1);
          printf("Enter mark2:");
          scanf("%d",&s[i].mark2);
          printf("Enter mark3:");
          scanf("%d",&s[i].mark3);
          s[i].total=s[i].mark1+s[i].mark1+s[i].mark1;
          s[i].per = (float)s[i].total/3;
      }
      printf("*****Student Information*******\n");
      for(i=0;i<3;i++)
      {
           printf("Roll Number:%d\n",s[i].regno);
           printf("Name:%s\n",s[i].name);
           printf("Gender:%s\n",s[i].gender); 
           printf("Mark1:%d\n",s[i].mark1);
           printf("Mark2:%d\n",s[i].mark2);
           printf("Mark3:%d\n",s[i].mark3);
           printf("Total:%d\n",s[i].total);
           printf("Percentage:%f\n",s[i].per); 
      }
      getch();
}

Output

Enter Roll Number:1
Enter Name: Jivansh
Enter Gender: Male
Enter Mark1: 45
Enter Mark2: 56
Enter Mark3: 65
Enter Roll Number:2
Enter Name: YESHA
Enter Gender: Female
Enter Mark1: 45
Enter Mark2: 56
Enter Mark3: 65
Enter Roll Number:3
Enter Name: Vidhi
Enter Gender: Female
Enter Mark1: 45
Enter Mark2: 56
Enter Mark3: 65
Roll Number:1
Name:Jivansh
Gender:Male
Mark1:45
Mark2:56
Mark3:65
Roll Number:2
Name:YESHA
Gender:Female
Mark1:45
Mark2:56
Mark3:65
Roll Number:3
Name:Vidhi
Gender:Female
Mark1:45
Mark2:56
Mark3:65
Create a structure named cricket that accepts cricketer name , team name, average and highest score. Enter values for 5 cricketer and display it using concept of array of structure.
#include<stdio.h>
#include<conio.h>
struct cricket
{
     char PlayerName[20];
     char TeamName[20];
     int average;
     int HighestScore;
};
void main()
{
     struct cricket player[5];
     int i;
     clrscr();
     printf("Enter Player Information:\n");
     for(i=0;i<5;i++)
     {
           printf("Enter Team name:");
           scanf("%s",player[i].TeamName);
           printf("Enter Player Name:");
           scanf("%s",player[i].PlayerName);
           printf("Enter batting Average:");
           scanf("%d",&player[i].average);
           printf("Enter Highest Score:");
           scanf("%d",&player[i].HighestScore);
      }
      printf("****** Player Information*******\n");
      for(i=0;i<5;i++)
      {
           printf("Team name:%s\n",player[i].TeamName);
           printf("Player Name:%s\n",player[i].PlayerName);
           printf("Average:%d\n",player[i].average);
           printf("Highest Score:%d\n",player[i].HighestScore);
      }
      getch();
}

Output

Enter Team Name: INDIA
Enter Player Name: Sachin
Enter Average: 49
Enter Highest Score: 200
Enter Team Name: INDIA
Enter Player Name: Sehwag
Enter Average: 39
Enter Highest Score: 182
Enter Team Name: INDIA
Enter Player Name: Gambhir
Enter Average: 40
Enter Highest Score: 145
Enter Team Name: INDIA
Enter Player Name: Yuvraj
Enter Average: 38
Enter Highest Score: 167
Enter Team Name: INDIA
Enter Player Name: Dhoni
Enter Average: 49
Enter Highest Score: 188
Team Name: INDIA
Player Name: Sachin
Average: 49
Highest Score: 200
Team Name: INDIA
Player Name: Sehwag
Average: 39
Highest Score: 182
Team Name: INDIA
Player Name: Gambhir
Average: 40
Highest Score: 145
Team Name: INDIA
Player Name: Yuvraj
Average: 38
Highest Score: 167
Team Name: INDIA
Player Name: Dhoni
Average: 49
Highest Score: 188
Program shows the use of Structure within Structure.
#include<stdio.h>
#include<conio.h>
struct Salary
{
     int EmpId;
     char name[20];
     int basic;
     float total;
     struct allowance
     {
          float da,hra,ma; 
     }a1;
     struct deduction
     {
           float it,pt; 
     }d1;
};
void main()
{
      struct Salary s[3];
      int i;
      clrscr();
      for(i=0;i<3;i++)
      {
             printf("Enter Employee Id:"); 
             scanf("%d",&s[i].EmpId);
             printf("Enter Employee Name:");
             scanf("%s",s[i].name);
             printf("Enter Basic:");
             scanf("%d",&s[i].basic);
             s[i].a1.da=(s[i].basic)*(0.56);
             s[i].a1.hra=(s[i].basic)*(0.10);
             s[i].a1.ma=(s[i].basic)*(0.08);
             s[i].d1.it=(s[i].basic)*(0.05);
             s[i].d1.pt=(s[i].basic)*(0.02); 
             s[i].total = s[i].basic+s[i].a1.da+s[i].a1.hra+s[i].a1.ma-s
             [i].d1.it-s[i].d1.pt; 
      }
      for(i=0;i<3;i++)
      {
             printf("Id:%d\n",s[i].EmpId);
             printf("Name:%s\n",s[i].name);
             printf("Basic:%d\n",s[i].basic);
             printf("DA:%f\n",s[i].a1.da);
             printf("HRA:%f\n",s[i].a1.hra);
             printf("MA:%f\n",s[i].a1.ma);
             printf("IT:%f\n",s[i].d1.it);
             printf("PT:%f\n",s[i].d1.pt);
             printf("Total:%f\n",s[i].total);
      }
      getch();
}

Download Projects


Download Programs