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 |
#include <iostream> using namespace std; int main() { int angle; // Input the angle in degrees cout << "Enter the angle in degrees: "; cin >> angle; // Determine the type of angle using a switch statement switch (angle) { case 0 ... 89: cout << "The angle is an acute angle." << endl; break; case 90: cout << "The angle is a right angle." << endl; break; case 91 ... 179: cout << "The angle is an obtuse angle." << endl; break; default: cout << "Invalid angle! Please enter an angle between 0 and 180 degrees." << endl; break; } return 0; } |
Explanation:
- Input:
- The program prompts the user to enter an angle in degrees.
- Switch Statement:
- The
switch
statement checks the value ofangle
:case 0 ... 89
: Matches any angle between 0 and 89 degrees. The program identifies this as an acute angle.case 90
: Matches exactly 90 degrees, identifying it as a right angle.case 91 ... 179
: Matches any angle between 91 and 179 degrees, identifying it as an obtuse angle.default
: Handles any input outside the valid range (0-180 degrees) by displaying an error message.
- The
- Output:
- The program prints whether the entered angle is acute, right, or obtuse based on the value provided.
Example Outputs:
Example 1: Acute Angle
Input:
- Angle:
45
Output:
1 2 |
Enter the angle in degrees: 45 The angle is an acute angle. |
Example 2: Right Angle
Input:
- Angle:
90
Output:
1 2 |
Enter the angle in degrees: 90 The angle is a right angle. |
Example 3: Obtuse Angle
Input:
- Angle:
120
Output:
1 2 |
Enter the angle in degrees: 120 The angle is an obtuse angle. |
Example 4: Invalid Angle
Input:
- Angle:
200
Output:
1 2 |
Enter the angle in degrees: 200 Invalid angle! Please enter an angle between 0 and 180 degrees. |