Grade your score in C++

Write a program to grade your score using if/else method in C++.

#include<iostream>
using namespace std;
int main(){
	int n;
	cout<<"Enter your marks(out of 100) : ";
	cin>>n;
	cout<<"Your grade is ";
	if(n>=90)cout<<"A";
	else if(n>=75)cout<<"B";
	else if(n>=65)cout<<"C";
	else if(n>=50)cout<<"D";
	else if(n>=35)cout<<"E";
	else cout<<"F";
	return 0;
}
grade your score using if-else statements
grade your score using if-else statements

The same program can be implemented using switch statement

#include<iostream>
using namespace std;
int main(){
	int n;
	cout<<"Enter your marks(out of 100) : ";
	cin>>n;
	cout<<"Your grade is ";
	switch(n/10){
		case 10:
		case 9 : cout<<"A";break;
		case 8 :
		case 7 : cout<<"B";break;
		case 6 : cout<<"C";break;
		case 5 : cout<<"D";break;
		case 4 : cout<<"E";break;
		case 3 :
		case 2 :
		case 1 :
		case 0 : cout<<"F";break;
		default : cout<<"Number is not in range";
	}
}
Grade your score using switch statement
Grade your score using switch statement

Leave a Reply