Using a switch statement write a program that evaluates a student's mark (0-100) to the respective grade. Grade boundaries are defined below
Less than 40: F
Between 40 and 49: E
Between 50 and 59: D
Between 60 and 69: C
Between 70 and 79: B
Greater or equal to 80: A Your program should ask the user to input the student's full name and mark (between 0 - 100) and then output the student's name and grade (A - F). You should also handle invalid grade values
#include <iostream>
using namespace std;
int main(){
char name[50];
int mark;
cout<<"Enter full name: ";
cin.getline(name, 50);
cout<<"Enter mark (between 0 - 100): ";
cin>>mark;
if(mark > 100 || mark < 0) mark = -10;
switch(mark / 10){
case 10:;
case 9:;
case 8:cout<<"Name: "<<name<<"\nGrade A\n"; break;
case 7:cout<<"Name: "<<name<<"\nGrade B\n"; break;
case 6:cout<<"Name: "<<name<<"\nGrade C\n"; break;
case 5:cout<<"Name: "<<name<<"\nGrade D\n"; break;
case 4:cout<<"Name: "<<name<<"\nGrade E\n"; break;
case 3:;
case 2:;
case 1:;
case 0:cout<<"Name: "<<name<<"\nGrade F\n"; break;
default: cout<<"Invalid marks\n";
}
return 0;
}
Comments