Write a program that prompts the user to enter three number grades (doubles are
ID: 3889859 • Letter: W
Question
Write a program that prompts the user to enter three number grades (doubles are probably best here). The program computes the average of the three grades and displays the letter grade for the three original grades and for the average. Letter grades are computed as follows (this requires if-statements):
A+:90-100 A:80-89 B:70-79 C:60-69 D:50-59 F: <50
ouput should resemble: Please enter three grades: 60 80 100 Average grade: 80 or A End of program.
or Please enter three grades: 75.5 81.3 19.7 Average grade: 58.83 or D End of program.
Since most grades are between 0 and 100 we shouldn’t proceed with the program if any of the grades are outside of that range. If any of the grades are < 0 or > 100, stop the program with an appropriate message. Example: Please enter three grades: -75.5 81.3 19.7 One of the grades is out of range. End of program.
Explanation / Answer
Answer- C++ program that prompts the user to enter three number grades, computes the average of the three grades and displays the letter grade for the three original grades and for the average:
#include<iostream>
using namespace std;
int main()
{
double marks[3];
float sum=0,avg;
int i;
cout<<" Please enter three grades:: ";
for(i=0; i<3; i++)
{
cout<<" Enter Marks[ "<<i+1<<" ] :: ";
cin>>marks[i];
sum=sum+marks[i];
}
avg=sum/3;
cout<<" Your Average Grade is :: ";
if(avg>=90 &&avg<=100)
cout<<avg<<" or A+";
else if(avg>=80 && avg<90)
cout<<avg<<" or A";
else if(avg>=70 && avg<80)
cout<<avg<<" or B";
else if(avg>=60 && avg<70)
cout<<avg<<"or C";
else if(avg>=50 && avg<60)
cout<<avg<<" or D";
else if(avg>=0 && avg<50)
cout<<avg<<" or F";
else
cout<<"Invalid Marks.";
return 0;
}
OUTPUT-
Please enter three grades::
Enter Marks 1 ] :: 60
Enter Marks 2 ] :: 80
Enter Marks[ 3 ] :: 100
Your Average Grade is :: 80 or A
...Program finished with exit code 0
Press ENTER to exit console.
Please enter three grades::
Enter Marks 1 ] :: 75.5
Enter Marks 2 ] :: 81.3
Enter Marks 3 ] :: 19.7
Your Average Grade is :: 58.8333 or D
...Program finished with exit code 0
Press ENTER to exit console.
OR For This OUTPUT-
Please enter three grades: -75.5 81.3 19.7 One of the grades is out of range. End of program.
#include<iostream>
using namespace std;
int main()
{
double marks[3];
float sum=0,avg;
int i;
cout<<" Please enter three grades:: ";
for(i=0; i<3; i++)
{
cout<<" Enter Marks[ "<<i+1<<" ] :: ";
cin>>marks[i];
}
for(i=0; i<3; i++)
{
if(marks[i]<0 || marks[i]>100)
{
cout<<"one of the grades is out of range";
exit(0);
}
else
sum=sum+marks[i];
}
avg=sum/3;
cout<<" Your Average Grade is :: ";
if(avg>=90 && avg<=100)
cout<<avg<<" or A+";
else if(avg>=80 && avg<90)
cout<<avg<<" or A";
else if(avg>=70 && avg<80)
cout<<avg<<" or B";
else if(avg>=60 && avg<70)
cout<<avg<<"or C";
else if(avg>=50 && avg<60)
cout<<avg<<" or D";
else if(avg>=0 && avg<50)
cout<<avg<<" or F";
else
cout<<"Invalid Marks.";
return 0;
}
OUTPUT-
Please enter three grades::
Enter Marks 1 ] :: -75.8
Enter Marks 2 ] :: 63.5
Enter Marks[ 3 ] :: 19.7
one of the grades is out of range
...Program finished with exit code 0
Press ENTER to exit console.
Please enter three grades::
Enter Marks 1 ] :: 50
Enter Marks 2 ] :: 50
Enter Marks 3 ] :: 50
Your Average Grade is :: 50 or D
...Program finished with exit code 0
Press ENTER to exit console.