Answer: The c++ program to calculate area and perimeter for different geometric shapes is given below.
#include <iostream>
using namespace std;
void circle();
void square();
void rectangle();
int main() {
string shape;
char choice;
do
{
cout<<"Enter the geometrical shape (circle, square, rectangle)." <<endl;
cin>>shape;
if(shape == "circle")
circle();
if(shape == "square")
square();
if(shape == "rectangle")
rectangle();
cout<<"Do you wish to continue (y/n)"<<endl;
cin>>choice;
if(choice == 'n')
cout<<"quitting..."<<endl;
}while(choice != 'n');
return 0;
}
void circle()
{
float area, circumference;
float r, pi=3.14;
cout<<"Enter the radius of the circle"<<endl;
cin>>r;
circumference = 2*pi*r;
area = pi*r*r;
cout<<"The circumference is "<<circumference<<" and the area of the circle is "<<area<<endl;
}
void square()
{
float area, perimeter;
float s;
cout<<"Enter the side of the square"<<endl;
cin>>s;
perimeter = 4*s;
area = s*s;
cout<<"The perimeter is "<<perimeter<< " and the area of the square is "<<area<<endl;
}
void rectangle()
{
float area, perimeter;
float b, h;
cout<<"Enter the breadth of the rectangle"<<endl;
cin>>b;
cout<<"Enter the height of the rectangle"<<endl;
cin>>h;
perimeter = 2*(b+h);
area = b*h;
cout<<"The perimeter is "<<perimeter<< " and the area of the rectangle is "<<area<<endl;
}
OUTPUT
Enter the geometrical shape (circle, square, rectangle).
circle
Enter the radius of the circle
3.56
The circumference is 22.3568 and the area of the circle is 39.7951
Do you wish to continue (y/n)
n
quitting...
Explanation:
The program performs for circle, square and rectangle.
A method is defined for every shape. Their respective method contains the logic for user input, computation and output.
Every method consists of the variables needed to store the dimensions entered by the user. All the variables are declared as float to ease the computation as the user can input decimal values also.
No parameters are taken in any of the methods since input is taken in the method itself.