In: Computer Science
Write a program which asks the user for grade, in integer form, from 0 to 100.
If the user's response is negative or greater than 100, insult them.
Otherwise, use if / else statements to print out the letter grade corresponding to the user's input.
Then, write a second version of the program which uses a switch instead of if else. (HINT: divide the user's response by 10)
C++ CODE::
#include <iostream>
using namespace std;
int main() {
int grade;
while(1)
{
cout<<"Enter grade: ";
cin>> grade;
if(grade >= 0 && grade <= 100)
{
break;
}
cout<<"\nError: Grade is not in required range.Please Enter again.."<<endl;
}
cout<<"Using If- else: "<<endl;
if(grade >= 90 && grade <= 100)
{
cout<<"Your grade is A"<<endl;
}
else if(grade >= 80 && grade<= 89)
{
cout<<"Your grade is B"<<endl;
}
else if(grade >= 70 && grade <= 79)
{
cout<<"Your grade is C"<<endl;
}
else if(grade>= 60 && grade<= 69)
{
cout<<"Your grade is D"<<endl;
}
else
{
cout<<"Your grade is F"<<endl;
}
cout<<"Using switch - case: "<<endl;
int choice = grade/10;
switch(choice)
{
case 9: cout<<"Your grade is A"<<endl;
break;
case 8: cout<<"Your grade is B"<<endl;
break;
case 7: cout<<"Your grade is C"<<endl;
break;
case 6: cout<<"Your grade is D"<<endl;
break;
default : cout<<"Your grade is F"<<endl;
}
}
SCREENSHOT::