Write a Simple program in C++ to calculate the area of Circle, Square, and Rectangle using switch statement.
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 35 36 37 38 39 40 41 | #include <iostream> using namespace std; int main() { int choice; float radius, side, length, width, area; cout << "Choose a shape to calculate its area:" << endl; cout << "1. Circle" << endl; cout << "2. Square" << endl; cout << "3. Rectangle" << endl; cin >> choice; switch(choice) { case 1: cout << "Enter the radius of the circle: "; cin >> radius; area = 3.14159 * radius * radius; cout << "Area of the circle: " << area << endl; break; case 2: cout << "Enter the side length of the square: "; cin >> side; area = side * side; cout << "Area of the square: " << area << endl; break; case 3: cout << "Enter the length and width of the rectangle: "; cin >> length >> width; area = length * width; cout << "Area of the rectangle: " << area << endl; break; default: cout << "Invalid choice." << endl; } return 0; } |
Possible Outputs:
Output#1 Choosing Circle:
Choose a shape to calculate its area:
1. Circle
2. Square
3. Rectangle
1
Enter the radius of the circle: 5
Area of the circle: 78.5398
Output#2 Choosing Square:
Choose a shape to calculate its area:
1. Circle
2. Square
3. Rectangle
2
Enter the side length of the square: 7
Area of the square: 49
Output#3 Choosing Rectangle:
Choose a shape to calculate its area:
1. Circle
2. Square
3. Rectangle
3
Enter the length and width of the rectangle: 4 9
Area of the rectangle: 36
Output#4 Invalid Choice (e.g., selecting option 4):
Choose a shape to calculate its area:
1. Circle
2. Square
3. Rectangle
4
Invalid choice.