How to Find Area of a Circle with Diameter in Python | Functions
How to find area of a circle with diameter in Python using functions.
In this article, you will learn how to find area of a circle with diameter in Python using functions.
Example
Enter the radius of the circle::
10
Diameter = 20.0 units
Circumference = 62.800000000000004 units
Area = 314.0 sq. units
Standard Formula
D = 2 * r
C = 2 * PI * r
A = PI * r2
r = radius of the circle
D = diameter of the circle
C = circumference of the circle
A = area of the circle
You should have knowledge of the following topics in Python programming to understand this program:
- Python
input()
function
1. By using the default value of PI = 3.14
# How to Find Area of a Circle with Diameter in Python
print("Enter the radius of the circle::\n")
r, d, c, a = float(input()), None, None, None
# r = radius, d = diameter, c = circumference, a = area
# It will calculate the diameter, circumference, and area
d = 2 * r
c = 2 * 3.14 * r
a = 3.14 * (r * r)
# It will print the final output
print("Diameter = ", d, "units")
print("Circumference = ", c, "units")
print("Area = ", a, " sq. units")
Output
Enter the radius of the circle::
10
Diameter = 20.0 units
Circumference = 62.800000000000004 units
Area = 314.0 sq. units
Explanation
In the above program, we have taken input 10
from the user via the system console. Then we applied the standard formula to calculate the diameter, circumference, and area.
Standard formulas are given at the top of the article near the example portion can see there.
After the whole calculation, It will return these values following:
- Diameter = 20.0 units
- Circumference = 62.800000000000004 units
- Area = 314.0 sq. units
2. By using Math.PI constant
# How to Find Area of a Circle with Diameter in Python using math.pi constant
# Import math Library
import math
print("Enter the radius of the circle::\n")
r, d, c, a = float(input()), None, None, None
# r = radius, d = diameter, c = circumference, a = area
# It will calculate the diameter, circumference, and area
d = 2 * r
c = 2 * math.pi * r
a = math.pi * (r * r)
# It will print the final output
print("Diameter = ", d, "units")
print("Circumference = ", c, "units")
print("Area = ", a, " sq. units")
Output
Enter the radius of the circle::
10
Diameter = 20.0 units
Circumference = 62.83185307179586 units
Area = 314.1592653589793 sq. units
How to Find Area of a Circle with Diameter in Python using Functions
# How to Find Area of a Circle with Diameter in Python using Functions
# To calculate the diameter
def Diameter(r):
return 2 * r
# To calculate the circumference
def Circumference(r):
return 2 * 3.14 * r
# To calculate the area
def Area(r):
return 3.14 * (r * r)
# It's the driver code
print("Enter the radius of the circle::\n")
r = float(input()) # r = radius
# It will print the final output
print("Diameter = ", Diameter(r), "units")
print("Circumference = ", Circumference(r), "units")
print("Area = ", Area(r), " sq. units")