In: Computer Science
in C++ use loopDW.cpp below
Do While Loop Exercise
#include <iostream> using namespace std; int main() { int mark; char grade; cout << "Please enter an integer mark: " << endl; // Please add a do-while loop below to make sure a valid mark // between [0, 100] is entered. if (mark >= 85) { grade = 'A'; cout << "Great Mark. " << endl; } else if (mark >= 75 && mark < 85) { grade = 'B'; cout << "Good Mark. " << endl; } else if (mark >= 65 && mark < 75) { grade = 'C'; cout << "Average Mark. " << endl; } else if (mark >= 50 && mark < 65) { grade = 'D'; cout << "You passed the class. " << endl; } else { grade = 'F'; cout << "You failed the class. " << endl; } cout << "The corresponding character grade is " << grade << endl; return 0; } // end program
Code:
#include <iostream>
using namespace std;
int main()
{
int mark;
char grade;
cout << "Please enter an integer mark: "
<< endl;
do{
//taking input from user
cin>>mark;
//if mark is greaterthan 100 or
lessthan 0
//then print the message
if(mark>100 || mark<0){
cout<<"Invalid input, marks [1 -100]"<<endl;
}
//this loop iterates until user enters valid
marks
}while(mark>100 || mark<0);
if (mark >= 85)
{
grade = 'A';
cout << "Great Mark. "
<< endl;
}
else if (mark >= 75 && mark < 85)
{
grade = 'B';
cout << "Good Mark. " <<
endl;
}
else if (mark >= 65 && mark < 75)
{
grade = 'C';
cout << "Average Mark. "
<< endl;
}
else if (mark >= 50 && mark < 65)
{
grade = 'D';
cout << "You passed the class.
" << endl;
}
else
{
grade = 'F';
cout << "You failed the class.
" << endl;
}
cout << "The corresponding character grade is " << grade << endl;
return 0;
}
// end program
Output:
Code Screenshot:
Code Snippet:
#include <iostream>
using namespace std;
int main()
{
int mark;
char grade;
cout << "Please enter an integer mark: " << endl;
do{
//taking input from user
cin>>mark;
//if mark is greaterthan 100 or lessthan 0
//then print the message
if(mark>100 || mark<0){
cout<<"Invalid input, marks [1 -100]"<<endl;
}
//this loop iterates until user enters valid marks
}while(mark>100 || mark<0);
if (mark >= 85)
{
grade = 'A';
cout << "Great Mark. " << endl;
}
else if (mark >= 75 && mark < 85)
{
grade = 'B';
cout << "Good Mark. " << endl;
}
else if (mark >= 65 && mark < 75)
{
grade = 'C';
cout << "Average Mark. " << endl;
}
else if (mark >= 50 && mark < 65)
{
grade = 'D';
cout << "You passed the class. " << endl;
}
else
{
grade = 'F';
cout << "You failed the class. " << endl;
}
cout << "The corresponding character grade is " << grade << endl;
return 0;
}
// end program