C++ Program to Calculate the Area and Perimeter of Different Shapes
This page contains a C++ program that calculates the area and perimeter of a circle, rectangle, and triangle. The program uses basic geometric formulas to perform these calculations.
Program Explanation
The C++ program to calculate the area and perimeter of different shapes follows these steps:
- Include necessary headers: Include the
<iostream>
and<cmath>
headers for input/output functions and mathematical functions. - Define functions: Create separate functions for calculating the area and perimeter of each shape.
- Main Function: The
main
function is the entry point of the program, which calls these functions and displays the results.
C++ Program Code
/*
* C++ program to calculate the area and perimeter of different shapes.
*/
#include <iostream>
#include <cmath>
using namespace std;
// Function prototypes
void calculateCircle();
void calculateRectangle();
void calculateTriangle();
int main() {
// Calculate and display area and perimeter for different shapes
calculateCircle();
calculateRectangle();
calculateTriangle();
return 0;
}
/**
* Function to calculate and display the area and circumference of a circle.
*/
void calculateCircle() {
double radius;
cout << "Enter the radius of the circle: ";
cin << radius;
double area = M_PI * radius * radius;
double circumference = 2 * M_PI * radius;
cout << "Area of the circle: " << area << endl;
cout << "Circumference of the circle: " << circumference << endl;
}
/**
* Function to calculate and display the area and perimeter of a rectangle.
*/
void calculateRectangle() {
double length, width;
cout << "Enter the length and width of the rectangle: ";
cin << length << width;
double area = length * width;
double perimeter = 2 * (length + width);
cout << "Area of the rectangle: " << area << endl;
cout << "Perimeter of the rectangle: " << perimeter << endl;
}
/**
* Function to calculate and display the area and perimeter of a triangle.
*/
void calculateTriangle() {
double a, b, c;
cout << "Enter the three sides of the triangle: ";
cin << a << b << c;
double s = (a + b + c) / 2;
double area = sqrt(s * (s - a) * (s - b) * (s - c));
double perimeter = a + b + c;
cout << "Area of the triangle: " << area << endl;
cout << "Perimeter of the triangle: " << perimeter << endl;
}
Program Details
The program contains the following:
calculateCircle
Function: This function prompts the user for the radius of a circle, calculates the area using the formulaπr2
, and the circumference using the formula2πr
.calculateRectangle
Function: This function prompts the user for the length and width of a rectangle, calculates the area using the formulalength × width
, and the perimeter using the formula2(length + width)
.calculateTriangle
Function: This function prompts the user for the three sides of a triangle, calculates the area using Heron’s formulasqrt(s(s-a)(s-b)(s-c))
wheres = (a + b + c) / 2
, and the perimeter as the sum of the sides.main
Function: The entry point of the program where the shape-specific functions are called to perform the calculations and display the results.
Running the Program
To run the program:
- Save the code in a file named
shape_calculations.cpp
. - Compile the program using the command:
g++ shape_calculations.cpp -o shape_calculations
. - Run the compiled program using the command:
./shape_calculations
.
Explanation of the C++ Program:
- Header Inclusions: The program includes
<iostream>
for input/output functions and<cmath>
for mathematical functions. - Function Prototypes: The program declares prototypes for three functions:
calculateCircle
,calculateRectangle
, andcalculateTriangle
. - Main Function:
- Calls each of the shape-specific functions to perform the calculations and display the results.
- calculateCircle Function:
- Prompts the user to enter the radius of the circle.
- Calculates the area using the formula πr2\pi r^2 and the circumference using the formula 2πr2 \pi r.
- Displays the calculated area and circumference.
- calculateRectangle Function:
- Prompts the user to enter the length and width of the rectangle.
- Calculates the area using the formula length×width\text{length} \times \text{width} and the perimeter using the formula 2(length+width)2(\text{length} + \text{width}).
- Displays the calculated area and perimeter.
- calculateTriangle Function:
- Prompts the user to enter the three sides of the triangle.
- Calculates the semi-perimeter ss using the formula s=a+b+c2s = \frac{a + b + c}{2}.
- Calculates the area using Heron’s formula s(s−a)(s−b)(s−c)\sqrt{s(s-a)(s-b)(s-c)}.
- Calculates the perimeter as the sum of the sides a+b+ca + b + c.
- Displays the calculated area and perimeter.