1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> using namespace std; int main() { int gradePoint; // Input the grade point cout << "Enter the grade point (0-4): "; cin >> gradePoint; // Determine the letter grade using a switch statement switch (gradePoint) { case 4: cout << "The letter grade is A." << endl; break; case 3: cout << "The letter grade is B." << endl; break; case 2: cout << "The letter grade is C." << endl; break; case 1: cout << "The letter grade is D." << endl; break; case 0: cout << "The letter grade is F." << endl; break; default: cout << "Invalid grade point! Please enter a number between 0 and 4." << endl; break; } return 0; } |
Explanation:
- Input:
- The program prompts the user to enter a grade point between 0 and 4.
- Switch Statement:
- The
switch
statement checks the value ofgradePoint
. - Each
case
corresponds to a specific letter grade:case 4
: Outputs “A.”case 3
: Outputs “B.”case 2
: Outputs “C.”case 1
: Outputs “D.”case 0
: Outputs “F.”
- The
default
case handles any input outside the range of 0-4, displaying an error message.
- The
- Output:
- Depending on the input, the program prints the corresponding letter grade.
1 |
Example Outputs:
Example 1: Valid Input
Input:
- Grade Point:
4
Output:
1 2 |
Enter the grade point (0-4): 4 The letter grade is A. |
Example 2: Valid Input
Input:
- Grade Point:
2
Output:
1 2 |
Enter the grade point (0-4): 2 The letter grade is C. |
Example 3: Invalid Input
Input:
- Grade Point:
5
Output:
1 2 |
Enter the grade point (0-4): 5 Invalid grade point! Please enter a number between 0 and 4. |