11/15/2015

C++ Program to Calculate Area of Circle Rectangle and Triangle Using Switch Statement

Write a C++ Program to Calculate Area of Circle Rectangle and Triangle Using Switch Statement. In this one problem, we have a three problem, so we have to use a Switch Case Statement for handle this problem. So according to the user Input our problem will display an answer. So in Switch case statement there will be total 3 test cases, and according to the input, particular case will be performed and user needs to enter the details for specific switch case, and press enter to display a result.

Logic to Calculate Area of Circle Rectangle and Triangle


Below are the formula to find the Area of Circle, Rectangle and Triangle. So first we are going to know the formulas and then after that we will implement in our program using switch case. So first we need to find the value of PI for area of radius, and for area of rectangle we need to length and width.

C++ Program to Calculate Area of Circle Rectangle and Triangle Using Switch Statement

Now the most important is to find the area of Triangle with 3 sides, for this we will Use Heron's formula with all three sides a,b and c of Triangle and divide by 2 after that we will apply a formula. Below is the C++ Program to Calculate Area of Circle Rectangle and Triangle Using Switch Statement. 

Area of Circle Rectangle and Triangle Formulas


Area of Circle: 3.14*r*r (r=Radius)

Area of Rectangle: a*b

Area of Triangle: sqrt(s*(s-a)*(s-b)*(s-c));

Where S=(a+b+c)/2; 

C++ Program to Calculate Area of Circle Rectangle and Triangle Using Switch Statement


#include<iostream>
#include<math.h>

using namespace std;
int main()
{
/*Visit: https://www.programmingwithbasics.com/2015/11/write-c-to-calculate-area-of-circle.html*/
float a, b, c, s, radius, area;

int ch;

cout<<"1.Area Of Circle";
cout<<"\n2.Area Of Rectangle";
cout<<"\n3.Area Of Triangle \n";
cout<<"\nEnter Your Choice :";

cin>>ch;

switch(ch)
{
case 1:
{
cout<<"\nEnter the Radius of Circle: ";
cin>>radius;
area=3.14159*radius*radius;
cout<<"Area of Circle = "<<area<<endl;
break;
}
case 2:
{
cout<<"\nEnter the Length and Breadth of Rectangle:";
cin>>a>>b;
area=a*b;
cout<<"Area of Rectangle = "<<area<<endl;
break;
}
case 3:
{
cout<<"\nEnter All Three Sides of Triangle with 3 Sides:";
cin>>a>>b>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of Triangle = "<<area<<endl;
break;
}
default: cout<<"\n Invalid Choice Try Again...!!!";
break;
}
return 0;
}

The Output of Area of Circle Rectangle and Triangle


The Output of Area of Circle Rectangle and Triangle

Check This Out

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: