In: Computer Science
C++ Design and implement a program (name it ComputeAreas) that defines four methods as follows: Method squareArea (double side) returns the area of a square. Method rectangleArea (double width, double length) returns the area of a rectangle. Method circleArea (double radius) returns the area of a circle. Method triangleArea (double base, double height) returns the area of a triangle. In the main method, test all methods with different input value read from the user. Document your code and properly label the input prompts and the outputs as shown below. Sample run: Square side: 5.1 Square area: 26.01 Rectangle width: 4.0 Rectangle length: 5.5 Rectangle area: 22.0 Circle radius: 2.5 Circle area: 19.625 Triangle base: 6.4 Triangle height: 3.6 Triangle area: 11.52
SOURCE CODE:
#include <iostream>
using namespace std;
double squareArea(double side) //method to calculate
squareArea
{
return side*side;
}
double rectangleArea (double width, double length) //method to
calculate rectangleArea
{
return width*length;
}
double circleArea (double radius) //method to calculate
circleArea
{
return 3.14*radius*radius;
}
double triangleArea (double base, double height) ////method to
calculate triangleArea
{
return 0.5*base*height;
}
int main()
{
double side,width,length,radius,base,height;
//variables for storing input values
cout <<"Square side:";
cin >>side; //reading side from user
cout <<"Square area:
"<<squareArea(side)<<endl; //calling squareArea
method
cout <<"Rectangle width:";
cin >>width; //reading width from
user
cout <<"Rectangle length:";
cin >>length;//reading length from
user
cout <<"Rectangle area:
"<<rectangleArea(width,length)<<endl; //calling
rectangleArea method
cout <<"Circle radius:";
cin >>radius; //reading radius from
user
cout <<"Circle
area:"<<circleArea(radius)<<endl; //calling circleArea
method
cout <<"Triangle base:";
cin >>base; //reading base from user
cout <<"Triangle height:";
cin >>height; //reading height from
user
cout <<"Triangle
area:"<<triangleArea(base,height)<<endl; //calling
triangleArea method
return 0;
}
CODE
SCREENSHOT:
OUTPUT: