Python Program to Calculate Diameter, Area and Perimeter of a Circle

Area, Perimeter of Circle

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


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


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. Python Program & Output to calculate diameter, area and perimeter of a circle

Code has been copied
# **************************************
#       alphabetacoder.com
# Python program to calculate diameter,
# area and perimeter of a circle
# **************************************

import math

# take input
r = float(input("Enter the radius of a circle: "))

# calculate diameter
d = 2 * r
# calculate area
a = math.pi * r * r
# calculate perimeter
p = 2 * math.pi * r

# display result upto 3 decimal places
print("Diameter: ", round(d, 3))
print("Area: ", round(a, 3))
print("Perimeter: ", round(p, 3))

Output


Enter the radius of a circle: 7

Diameter: 14.0

Area: 153.938

Perimeter: 43.982