C++ Program to Calculate Diameter, Area and Perimeter of a Circle

Area, Perimeter of Circle

C++ program to calculate diameter, area and perimeter of a circle has been given below. Suppose, the radius of a circle is r unit, the area and perimeter of that circle would be (pi * r^2) unit^2 and (2 * pi * r) unit, respectively while the diameter would be 2 * r. The approximate value of pi is 3.141592.


For example, if the radius of a circle is 5 cm, then area of the circle would be (pi * 5^2) = 78.54 cm^2 while perimeter and diamerter would be (2 * pi * r) = 31.42 cm and (2 * r) = 10 cm respectively.


The algorithm, pseudocode, and time-complexity of the program have also been covered below.





1. Algorithm to calculate diameter, area and perimeter of a circle


1. Take the radius r of a circle as input.

2. Compute d = 2 * r

3. Compute a = pi * r^2

4. Compute p = 2 * pi * r

5. Declare d as the diameter, a as the area and p as the perimeter of that circle.




2. Pseudocode to calculate diameter, area and perimeter of a circle


Input : Radius r of a circle

Output : Diameter D, Area A, Perimeter P of the circle

1. Procedure diameterAreaPerimeter(r):

2. D := 2 * r

3. A := pi * r^2

4. P := 2 * pi * r

5. Return D, A, P

6. End Procedure





3. Time complexity to calculate diameter, area and perimeter of a circle


Time Complexity: O(1)




4. C++ Program & Output to calculate diameter, area and perimeter of a circle

Code has been copied
/***********************************
          alphabetacoder.com
 C++ program to calculate diameter,
 area and perimeter of a circle
***********************************/

#include <iostream>
#define pi 3.141592

using namespace std;

int main() {
    // declare variables
    float r, d, a, p;

    // take input
    cout << "Enter the radius of a circle: ";
    cin >> r;

    // calculare diameter
    d = 2 * r;
    // calculate area
    a = pi * r * r;
    // calculate perimeter
    p = 2 * pi * r;

    // display result
    cout << "Diameter: " << d << endl;
    cout << "Area: " << a << endl;
    cout << "Perimeter: " << p << endl;

    return 0;
}

Output


Enter the radius of a circle: 6

Diameter: 12

Area: 113.097

Perimeter: 37.6991