Skip to main content

ProwessApps

Learn · Practice · Excel

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

C++ Code Example — Conditional Programs

ADVERTISEMENT

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

Objective

Write a C++ program to calculate student grades based on calculated percentage marks.

Algorithm / Approach

  1. Read marks for 5 subjects.
  2. Calculate the percentage: per = (m1+m2+m3+m4+m5) / 5.0.
  3. Use an if (per >= 80) for Grade A.
  4. Use cascading else if statements for lower grades (70, 60, 50).
  5. Use a final else for Grade E.
main.cpp
#include<iostream>
using namespace std;
int main() {
 int m1,m2,m3,m4,m5;
 float per;
 cout<<"Enter Marks Subject 1: ";
 cin>>m1;
 cout<<"Enter Marks Subject 2: ";
 cin>>m2;
 cout<<"Enter Marks Subject 3: ";
 cin>>m3;
 cout<<"Enter Marks Subject 4: ";
 cin>>m4;
 cout<<"Enter Marks Subject 5: ";
 cin>>m5;
 per = (m1+m2+m3+m4+m5)/5.0;
 cout<<"Your Marks % is "<< per<< endl;
 if(per>=80)
  cout<<"Your Grade is A \n";
 else if(per>= 70)
  cout<<"Your Grade is B \n";
 else if(per>= 60)
  cout<<"Your Grade is C \n";
 else if(per>= 50)
  cout<<"Your Grade is D \n";
 else
  cout<<"Your Grade is E \n";
return 0;
}

Expected Output

Enter Marks Subject 1: 90
Enter Marks Subject 2: 91
Enter Marks Subject 3: 92
Enter Marks Subject 4: 92
Enter Marks Subject 5: 90
Your Marks % is 91
Your Grade is A

Explanation of the Program

  • Cascading else if structures are highly sensitive to their sequential order.
  • Because the program checks from top to bottom and stops at the first true condition, we MUST check the highest percentages first. If we checked per &gt;= 50 first, a student with 90% would trigger that condition and be incorrectly awarded a D grade!

Complexity

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