Q:

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

0

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.

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; 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

using namespace std;
int main()
{
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;
}

 

Output:

1.Area Of Circle

2.Area Of Rectangle

3.Area Of Triangle 

Enter Your Choice :2

Enter the Length and Breadth of Rectangle:15 26 5

15

Area of Rectangle = 375

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to find a Grade of Given Marks Using S... >>
<< C++ Program For Arithmetic Operations Using Switch...