Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade

Table of Contents

C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade

  1. #include <stdio.h> – This line includes the standard input/output library in the program. This is necessary to use the printf() and scanf() functions.
  2. int main() – This line declares the main function of the program. It is the entry point of the program where the execution begins.
  3. float physics, chemistry, biology, math, computer; float percentage; – This line declares five float variables physics, chemistry, biology, math and computer, which will hold the marks for the five subjects, and a float variable percentage which will hold the calculated percentage.
  4. printf("Enter marks of Physics: "); – This line prints a message on the screen asking the user to enter the marks of Physics.
  5. scanf("%f", &physics); – This line reads the input entered by the user for Physics and stores it in the variable physics.
  6. Steps 4 and 5 are repeated for each of the remaining subjects.
  7. 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.
  8. printf("Percentage = %.2f\n", percentage); – This line prints the calculated percentage on the screen with a precision of 2 decimal points.
  9. 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.
  10. 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