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; } |
C++ Program: [Switch] Area Calculation: Circle, Square, Rectangle using switch statement
Write a Simple program in C++ to calculate the area of Circle, Square, and Rectangle using switch statement.