Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to ask user to enter marks of 5 subjects and calculate percentage then print GRADE according to marks in percentage

C Code Example — Conditional Programs

ADVERTISEMENT

C Program to ask user to enter marks of 5 subjects and calculate percentage then print GRADE according to marks in percentage

Objective

Write a C program to calculate the percentage of 5 subjects and assign a Grade using an if-else-if ladder.

Algorithm / Approach

  1. Read 5 integer marks from the user.
  2. Calculate the percentage: per = (m1+m2+m3+m4+m5) / 5.0f.
  3. Use an if-else-if ladder to assign grades based on descending thresholds.
  4. Check if (per >= 70) for Grade A.
  5. Check else if (per >= 60) for Grade B.
  6. Continue this pattern down to Grade D, and use a final else for FAIL.
main.c
#include<stdio.h>
int main( ) {
 int m1,m2,m3,m4,m5;
 float per;
 printf("Enter Marks Subject 1: ");
 scanf("%d",&m1);
 printf("Enter Marks Subject 2: ");
 scanf("%d",&m2);
 printf("Enter Marks Subject 3: ");
 scanf("%d",&m3);
 printf("Enter Marks Subject 4: ");
 scanf("%d",&m4);
 printf("Enter Marks Subject 5: ");
 scanf("%d",&m5);
 per = (m1+m2+m3+m4+m5)/5.0f;
 printf("YOUR PERCENTGE : %f\n",per);
 if(per>=70){
  printf("YOUR GRADE IS : A\n");
 }
 else if(per>=60){
  printf("YOUR GRADE IS : B\n");
 }
 else if(per>=50){
  printf("YOUR GRADE IS : C\n");
 }
 else if(per>=40){
  printf("YOUR GRADE IS : D\n");
 }
 else{
  printf("YOUR GRADE IS : FAIL\n");
 }
 return 0;
}

Expected Output

Enter Marks Subject 1: 55
Enter Marks Subject 2: 67
Enter Marks Subject 3: 50
Enter Marks Subject 4: 68
Enter Marks Subject 5: 78
YOUR PERCENTAGE : 63.60
YOUR GRADE IS : B

Explanation of the Program

  • Notice the calculation / 5.0f. Because the marks are integers, dividing by a pure 5 would perform integer division (discarding decimals). By dividing by 5.0f (a float), we force floating-point division to get an accurate decimal percentage.
  • When using an if-else-if ladder for ranges, always check the HIGHEST range first and work downwards. If you checked per >= 40 first, a student with 90% would trigger that block and falsely receive a Grade D.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT