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.
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++
Similar to the Find Area of a Triangle Square Circle Rectangle
- C++ Program To Convert Celsius To Fahrenheit And Vice Versa Using Switch Case
- C++ Program for Arithmetic Operations Using Switch Case
- C++ Program to Calculate Grade of Student Using Switch Statement
- C Program to Convert Temperature from Fahrenheit to Celsius and Vice Versa
- C Program for Find a Grade of Given Marks Using Switch Case
0 Comments: