# C++ program to calculate area of a circle,a rectangle or a triangle depending upon user's choice

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

void main()
{
clrscr(); //to clear the screen
float a,b,c,s,r,area;
int ch;
cin>>ch;

switch(ch)
{
case 1:
{
cin>>r;
area=3.14*r*r;
break;
}
case 2:
{
cin>>a>>b;
area=a*b;
break;
}
case 3:
{
cout<<“nEnter three sides of the triangle:”;
cin>>a>>b>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
break;
}
default: cout<<“nWrong choice…!!!”;
break;
}

cout<<“Area=”<<area;
getch(); //to stop the screen
}

### 11 thoughts on “C++ program to calculate area of a circle,a rectangle or a triangle depending upon user's choice”

1. That was great.
Can you please tell me what I did wrong here??

//Give the Area of a Square or a Triangle.
#include // tells the compiler to include this file
#include // tells the compiler to include this file
#include// tells the compiler to include this file
#include// tells the compiler to include this file
using namespace std; //use the standart namespace

float findSArea(float s); // function that calculates Square area
float findTArea(float b, float h); // function that calculates Triangle area

int main() // start of main function
{

float side, base, height; //input: data for calc
float square, triangle;
char letter;
char S,T;
float num;

cout<<"Please enter S for Square or T for Triangle (in capital letters): "<>letter;

// decide what to do
if ((letter==S) || (letter==T)) {

if (letter==S) {
cout<>side;
num=findSArea(s); // function call to calculate the Square area
cout<>num;
}
if (letter==T) {
cout<>base>>height;
num=findTArea(b,h); // function call to calculate the Triangle area
cout<>num;
}
}
else
{
cout<< "invalid character! ";
}

system ("pause");
return 0; }

// Compute the area of a Square with side s
//Pre: s is defined
//Post: returns area

float findSArea(float s)
{return (pow (s,2)); }

// Compute the area of a triangle with base b & height h
//Pre: b & H are defined
//Post: returns area

float findTArea(float b,h)
{return (0.5 * b * h); }

2. Write a c++ program that will:

â€¢ Provide 2 options to the user as below:
ïƒ¼ Enter 1 to calculate the area of Rectangle
ïƒ¼ Enter 2 to calculate the area of a Trapezoid

â€¢ If the user enters 1, then the program should ask the user to enter the width and length of the rectangle and then show the result after calculation. Formula for calculating Area of rectangle is
Area = width x Length.

â€¢ If the user enters 2, then the program should ask the user to enter the base1, base2, and height of the trapezoid and then show the result after calculation. Formula for calculating Area of trapezoid is
Area =

â€¢ You must implement two different functions for calculating areas of rectangle and trapezoid.
â€¢ After taking the choice from the user in form of 1 or 2, the relevant function should be called.
â€¢ After showing the output to the user, you need to ask the user if he/she wants to do another calculation. If the user presses y or Y, then the program should ask the user again to enter the choice of shape otherwise the program should be terminated.

Screenshot of the program execution is given below for both shapes:

Sample output:

3. please bro tell me the solution of this problem
i dont wanna code but instructions please

4. a c++ program to calculate area of the circle , square and rectangle using runtime polymorphism anyone plz reply for it thanks in advance

5. #include
using namespace std;
void main()
{int a,b,c,d,e;
for(a=1;a<=4;a++)
{switch(a)
{case 1:

cout<<a<<endl;
case 2:
for(b=2;b<=3;b++)
{cout<<b<<"\t";}
for(e=1;e<=4;e++)
{cout<<"*";}
cout<<endl;break;

case 3:
for(c=4;c<=6;c++)
{cout<<c<<"\t";}
cout<<endl;break;
case 4:
for(d=7;d<=10;d++)
{cout<<d<<"\t";}
cout<<endl;break;
default:
cout<<"lol"<<endl;

}

}} (why case 2 is printing 2 times)

6. Create a program that allows the user to select a shape he/she wants to compute the area. The program should accept either a lowercase or uppercase.

Note: Area should be presented in two decimal places.

Sample Output:

Area of Shapes
[S] – Square
[C] – Circle
[T] – Triangle
[R] – Rectangle