28/01/2023

C++ Program to Find Area of a Triangle / Square / Circle / Rectangle Using Switch Statement

Write a C++ Program to Find Area of a Triangle Square Circle Rectangle Using Switch Statement. In this one problem, we have four problems, Area of Triangle, Area of Square, Area of Circle and Area of Rectangle. We are going to solve this problem using a Switch Case Statement. So according to the user input, our problem will display an answer. 

There will be a total of 4 Choices for the user, according to the input of the user particular case will be performed. Here the first user has to make a choice to find the Area of a Triangle, the Area of a Square, the Area of a Circle and the Area of a Rectangle.

C++ Program to Find Area of a Triangle Square Circle Rectangle Using Switch Statement

After that according to the choice again user has to provide the necessary extra details to perform the operation. So first we must know the formulas of the given problem, and then we will implement them in our program using a switch case. So first we need to find the value of PI for the area of the radius,

Formulas for the Area of a Triangle, Square, Circle and Rectangle

Area of Triangle = (Base * Height) / 2.

Area of Square = Side * Side or Side2

Area of Circle = 3.14159 * radius * radius; or Ï€R2
 
Area of Rectangle = Length * Width


C++ Program to Find Area of a Triangle Square Circle Rectangle


#include<iostream>

#include<math.h>

using namespace std;
int main() {
  /*
  write a c++ program to find area of a triangle / square / circle / rectangle using switch statement.

  Visit: https://www.programmingwithbasics.com/2015/11/write-c-to-calculate-area-of-circle.html

  */

  float a, b, c, s, radius, area;

  int choice;

  cout << "Press 1 for Area of a Triangle";
  cout << "\nPress 2 for Area Of Square";
  cout << "\nPress 3 for Area Of Circle ";
  cout << "\nPress 4 for Area Of Rectangle\n";
  cout << "\nEnter Your Choice :";

  cin >> choice;

  switch (choice) {
  case 1: {
    cout << "\nEnter Base and Height of Triangle: ";
    cin >> a >> b;
    area = (a*b) / 2 ; 
    cout << "\nArea of Triangle = " << area << endl;
    break;
  }
  case 2: {
    cout << "\nEnter the Side Of Square: ";
    cin >> a;
    area = a * a;
    cout << "\nArea of Square = " << area << endl;
    break;
  }
  case 3: {
    cout << "\nEnter the Radius of Circle: ";
    cin >> radius;
    area = 3.14159 * radius * radius;
    cout << "\nArea of Circle = " << area << endl;
    break;
  }
  case 4: {
    cout << "\nEnter the Length and Width of Rectangle: ";
    cin >> a >> b;
    area = a * b;
    cout << "\nArea Of Rectangle = " << area << endl;
    break;
  }
  default:
    cout << "\n Invalid Choice!!! Choose Between 1 to 4";
    break;
  }
  return 0;
}

The Output of Find Area of a Triangle Square Circle Rectangle in C++


The Output of Find Area of a Triangle Square Circle Rectangle in C++

Similar to the Find Area of a Triangle Square Circle Rectangle

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: