In: Computer Science
Write a C++ program to ask the user to enter the shape type: square, sphere or circle and the appropriate dimensions of the shape.. The program should output the following information about the shape:
a.for a square. it output the area and perimeter.
b. for a sphere, it outputs the area.
c. fir a circle, it outputs the volume.
if the user input anything else, your program should output: "the program does not recognize this shape". Use const whenever it is appropriate (use the nested if)
area of sphere is 4*PI*r^2
area of circle is Pi*r^2 (Pi=3.1416)
code
#include<iostream>
using namespace std;
const float pi=3.14;
int main()
{
string type;
//Declare variables
float r;int s,perimeter;
double volume;
double area;
cout<<"Enter the shape type: ";
getline (cin, type);
if(type=="square"){
cout<<"Enter Side : ";
cin>>s;
//nested if
if(s>0){
area = 4*s;
perimeter = s*s;
cout<<"Area of Square : "<<area<<endl;
cout<<"Perimeter of Square : "<<perimeter;
}
}
if(type=="sphere"){
//Input radius
cout<<"Input radius: "<<endl;
cin>>r;
//Volume of sphere = 4/3(πr3)
volume=(4/3)*(pi*r*r*r);
cout<<"Volume of sphere:"<<volume<<endl;
//Area of sphere = πr2
area=pi*r*r;
cout<<"Area of Circle: "<<area<<endl;
}
if(type=="circle"){
cout<<"Enter radius : ";
cin>>r;
area = 3.14159*r*r;
cout<<"area of circle is : "<<area;
}
//remaining condition
else{
cout<<"The program does not recognize this shape.";
}
return 0;
}
//screenshot
//output:
-------------------------------------------------------------------------------------------------