Problem 22

author
By Supreme2023-03-15
Problem Statement :
Write a program to calculate average and total of 5 students for 3 subjects (use nested for loops)
Code:

#include <stdio.h>

int main(){

//22. Write a program to calculate average and total of 5 students for 3 //subjects (use nested for loops

int marks,j,i,sum=0,avg;

for(i=0;i<5;i++){
printf("\n\n\t\tEnter The Marks Of Student [%d] ", i+1);
printf("\n\t--------------------------------------------------\n");

for(j=0;j<3;j++){
printf("\n Enter The Marks Of Subject [%d] :", j+1);
scanf("%d",&marks);
sum = sum + marks;
}
avg = sum /3;
printf("\n___________________________________________________\n");
printf("\n\n Total Marks Of Student [%d] = %d", i+1, sum);
printf("\n Average Marks Of Student [%d] = %d", i+1, avg);

printf("\n\n________________________________________________________________");
}

return 0;
}

Description :
  • The program calculates the average and total of 5 students for 3 subjects using nested for loops.
  • It declares the necessary variables for the program, including marks, j, i, sum, and avg.
  • It uses two nested for loops, where the outer loop iterates over the students (i.e., 5 iterations) and the inner loop iterates over the subjects (i.e., 3 iterations).
  • Within the inner loop, the program prompts the user to enter the marks for each subject and calculates the sum of the marks for the current student.
  • After the inner loop finishes, the program calculates the average of the marks for the current student and prints the total marks and average marks for that student.
  • The program repeats this process for all 5 students using the outer loop.
  • Finally, the program ends and returns 0.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.