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:

  1. Include necessary headers: Include the <iostream> and <cmath> headers for input/output functions and mathematical functions.
  2. Define functions: Create separate functions for calculating the area and perimeter of each shape.
  3. 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 formula 2πr.
  • calculateRectangle Function: This function prompts the user for the length and width of a rectangle, calculates the area using the formula length × width, and the perimeter using the formula 2(length + width).
  • calculateTriangle Function: This function prompts the user for the three sides of a triangle, calculates the area using Heron’s formula sqrt(s(s-a)(s-b)(s-c)) where s = (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:

  1. Save the code in a file named shape_calculations.cpp.
  2. Compile the program using the command: g++ shape_calculations.cpp -o shape_calculations.
  3. Run the compiled program using the command: ./shape_calculations.

 

Explanation of the C++ Program:

  1. Header Inclusions: The program includes <iostream> for input/output functions and <cmath> for mathematical functions.
  2. Function Prototypes: The program declares prototypes for three functions: calculateCircle, calculateRectangle, and calculateTriangle.
  3. Main Function:
    • Calls each of the shape-specific functions to perform the calculations and display the results.
  4. 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.
  5. 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.
  6. 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.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)