This C++ program asks the user to choose a shape (triangle, square, circle or rectangle), reads the required dimensions and prints the area. It is a classic exercise for the switch statement, because each menu option becomes one case.
Area formulas used
| Shape | Area |
|---|---|
| Triangle (three sides a, b, c) | Heron’s formula: √(s(s − a)(s − b)(s − c)), where s = (a + b + c) / 2 |
| Square (side a) | a × a |
| Circle (radius r) | π × r × r |
| Rectangle (length l, width w) | l × w |
C++ program to find the area using switch
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const double PI = 3.14159265358979;
int choice;
cout << "1. Triangle" << endl;
cout << "2. Square" << endl;
cout << "3. Circle" << endl;
cout << "4. Rectangle" << endl;
cout << "Enter your choice (1-4): ";
cin >> choice;
switch (choice) {
case 1: {
double a, b, c;
cout << "Enter the three sides of the triangle: ";
cin >> a >> b >> c;
if (a + b <= c || a + c <= b || b + c <= a) {
cout << "These sides cannot form a triangle." << endl;
break;
}
double s = (a + b + c) / 2; // semi-perimeter
cout << "Area of the triangle = " << sqrt(s * (s - a) * (s - b) * (s - c)) << endl;
break;
}
case 2: {
double side;
cout << "Enter the side of the square: ";
cin >> side;
cout << "Area of the square = " << side * side << endl;
break;
}
case 3: {
double r;
cout << "Enter the radius of the circle: ";
cin >> r;
cout << "Area of the circle = " << PI * r * r << endl;
break;
}
case 4: {
double length, width;
cout << "Enter the length and width of the rectangle: ";
cin >> length >> width;
cout << "Area of the rectangle = " << length * width << endl;
break;
}
default:
cout << "Invalid choice. Please enter a number from 1 to 4." << endl;
}
return 0;
}Sample runs
1. Triangle 2. Square 3. Circle 4. Rectangle Enter your choice (1-4): 1 Enter the three sides of the triangle: 3 4 5 Area of the triangle = 6
1. Triangle 2. Square 3. Circle 4. Rectangle Enter your choice (1-4): 3 Enter the radius of the circle: 7 Area of the circle = 153.938
1. Triangle 2. Square 3. Circle 4. Rectangle Enter your choice (1-4): 4 Enter the length and width of the rectangle: 12.5 4 Area of the rectangle = 50

How the program works
- The menu is printed and the user’s choice is read into
choice. - The
switchjumps to the matchingcase. Each case has its own braces{ }so it can declare its own variables. - For the triangle, the program first checks the triangle inequality and then applies Heron’s formula using
sqrt()from<cmath>. breakends each case; any other number falls through todefault, which prints an error message.
Frequently asked questions
Why does each case have its own braces?
In C++ you cannot jump past a variable declaration into the same scope. Putting each case in its own block { } gives every case a separate scope for its variables.
Can I use if-else instead of switch?
Yes. A chain of if / else if works the same way, but switch is clearer when you compare one variable against several fixed values.
Why use double instead of int?
Lengths and areas are often not whole numbers (for example a radius of 2.5), and the area of a circle always involves π, so double keeps the decimals.





