New: 77 pattern programs with tested code in C, C++, Java and Python Explore patterns

C++ Program to Find the Area of a Triangle, Square, Circle and Rectangle Using Switch

Read summary with AI tools
Table of contents
  1. Area formulas used
  2. C++ program to find the area using switch
  3. Sample runs
  4. How the program works
  5. Frequently asked questions

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

ShapeArea
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
Sample run of the C++ area program: choice 1, triangle sides 3 4 5, area 6
Choosing 1 and entering the sides 3, 4 and 5 gives an area of 6 (Heron’s formula).

How the program works

  • The menu is printed and the user’s choice is read into choice.
  • The switch jumps to the matching case. 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>.
  • break ends each case; any other number falls through to default, 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.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.