C program to demonstrate the use of structures

This program finds the total marks of a student by using structures.


Explanation of the program


An emp structure is created with the variables name[], m1, m2, m3 and total, then a structure array s[3] is declared which has the type emp. The for loop is used to get the name and marks of three students and it will be stored in s[i].name, s[i].m1, s[i].m2, s[i].m3 respectively. The total marks is calculated by the statement s[i].total=s[i].m1+s[i].m2+s[i].m3. After reading the data of three students the name and total marks of each student is printed.

Program


#include<stdio.h>

#include<conio.h>

struct emp

{

char name[10];

int m1,m2,m3,total;

}s[3];

void main()

{

clrscr();

int i;

for(i=0;i<3;i++)

{

printf("\nEnter the name marks 1, 2 & 3 of student %d",i+1);

scanf("%s%d%d%d",&s[i].name,&s[i].m1,&s[i].m2,&s[i].m3);

s[i].total=s[i].m1+s[i].m2+s[i].m3;

}

for(i=0;i<3;i++)

{

printf("\nTotal mark of %s is %d\n",s[i].name, s[i].total);

}

getch();

}


Output





C program using structure to calculate the total marks of a student
C program using structure to calculate total marks

No comments: