<u>C++ program that computes the area and perimeter of a specified shape</u>
#include <iostream>
#include <cmath>
using namespace std;
void rectangle()
//Defining function for rectangle
{ int h,w;
cout << "Enter height: ";
//taking input
cin >> h;
cout << "Enter width: ";
cin >> w;
cout << "The perimeter of the rectangle is " <<2*h+ 2*w << " and the area is " <<h*w << endl; //printing output
}
void triangle() //Defining function for triangle
{ int s1,s2,s3,h,w;
cout << "Side 1: "; //Taking input
cin >> s1;
cout << "Side 2: ";
cin >> s2;
cout << "Side 3: ";
cin >> s3;
cout << "Enter the height: ";
cin >> h;
cout << "Enter the base length: ";
cin >> w;
cout << "The perimeter of the triangle is " <<s1+s2+s3 << " and the area is " <<(.5)*w*h << endl;
//printing output
}
void circle()//Defining Function for the circle
{
const double p=3.14;
int w;
cout << "Enter the radius: ";
//Taking input
cin >> w;
cout << "The perimeter of the circle is " << p*2*w << " and the area is " << p*w*w<< endl; //printing output
}
int main() //driver function
{
int s;
cout << "Enter the shape (1 for rectangle,2 for triangle, 3 for circle): ";
//Asking user for the shape
cin >> s;
switch(s) //checking which shape it chooses
{
case 1:
rectangle(); //If user type 1 ,then calling rectangle function
break;
case 2:
triangle(); //If user type 2 ,then calling triangle function
break;
case 3:
circle(); //If user type 3,then calling circle function
break;
default:
cout <<"Enter valid choice for shape"; //If user type other than 1,2,3
}
return 0;
}
<u>Output</u>
Enter the shape (1 for rectangle,2 for triangle, 3 for circle): 1
Enter height:2
Enter width: 3
The perimeter of the rectangle is 10 and the area is 6
<u />