Write a program in 'C-language', which
write the names of 20 students of a class
and in front of each name write the
average marks of four subjects also.
Answers
Answer:
#include <stdio.h>
#define MAX 2
struct Student{
char name[50];
int m[4];
};
int main()
{
struct Student s[10];
int i;
for(i=0; i < MAX ;i++)
{
printf("Enter Name of Student %d\n",i+1);
scanf("%s",s[i].name);
printf("Enter Marks 1 for %s\n",s[i].name);
scanf("%d",&s[i].m[0]);
printf("Enter Marks 2 for %s\n",s[i].name);
scanf("%d",&s[i].m[1]);
printf("Enter Marks 3 for %s\n",s[i].name);
scanf("%d",&s[i].m[2]);
printf("Enter Marks 4 for %s\n",s[i].name);
scanf("%d",&s[i].m[3]);
printf("\n");
}
printf("\n\nBelow are the average\n\n");
for(i=0;i<MAX;i++)
printf("%s : %d\n",s[i].name,(s[i].m[0]+s[i].m[1]+s[i].m[2]+s[i].m[3])/4);
return 0;
}
I have defined MAX as 2, you can increase the size to 10.
I havent catered for floating average, take care if you want decimal values.
Hope this helps, sample below output :
Enter Name of Student 1
Rajeev
Enter Marks 1 for Rajeev
90 Enter Marks 2 for Rajeev
45 Enter Marks 3 for Rajeev
67 Enter Marks 4 for Rajeev
89
Enter Name of Student 2
Varsha
Enter Marks 1 for Varsha
90
Enter Marks 2 for Varsha
33
Enter Marks 3 for Varsha
45
Enter Marks 4 for Varsha 78
Below are the average Rajeev : 72 Varsha : 61