Uncategorized
Table of Contents
C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade
#include <stdio.h>
– This line includes the standard input/output library in the program. This is necessary to use theprintf()
andscanf()
functions.int main()
– This line declares themain
function of the program. It is the entry point of the program where the execution begins.float physics, chemistry, biology, math, computer; float percentage;
– This line declares five float variablesphysics
,chemistry
,biology
,math
andcomputer
, which will hold the marks for the five subjects, and a float variablepercentage
which will hold the calculated percentage.printf("Enter marks of Physics: ");
– This line prints a message on the screen asking the user to enter the marks of Physics.scanf("%f", &physics);
– This line reads the input entered by the user for Physics and stores it in the variablephysics
.- Steps 4 and 5 are repeated for each of the remaining subjects.
percentage = (physics + chemistry + biology + math + computer) / 5.0;
– This line calculates the total marks obtained by the student by adding up the marks for each subject and then divides it by 5.0 to get the average percentage.printf("Percentage = %.2f\n", percentage);
– This line prints the calculated percentage on the screen with a precision of 2 decimal points.- The next block of code uses if-else statements to assign a grade to the student based on their percentage. The program will print the appropriate grade on the screen based on the percentage.
return 0;
– This line indicates that the program has executed successfully and returns the value 0 to the operating system.
#include <stdio.h> int main() { float physics, chemistry, biology, math, computer; float percentage; printf("Enter marks of Physics: "); scanf("%f", &physics); printf("Enter marks of Chemistry: "); scanf("%f", &chemistry); printf("Enter marks of Biology: "); scanf("%f", &biology); printf("Enter marks of Mathematics: "); scanf("%f", &math); printf("Enter marks of Computer: "); scanf("%f", &computer); percentage = (physics + chemistry + biology + math + computer) / 5.0; printf("Percentage = %.2f\n", percentage); if (percentage >= 90.0) { printf("Grade A"); } else if (percentage >= 80.0) { printf("Grade B"); } else if (percentage >= 70.0) { printf("Grade C"); } else if (percentage >= 60.0) { printf("Grade D"); } else if (percentage >= 40.0) { printf("Grade E"); } else { printf("Grade F"); } return 0; }
OUTPUT
Enter marks of Physics: 85
Enter marks of Chemistry: 78
Enter marks of Biology: 92
Enter marks of Mathematics: 90
Enter marks of Computer: 88
Percentage = 86.60
Grade B
Menu