Python Program to Find the Area of a Circle
Beginner · Basics
The area of a circle is the space inside it, found with the formula area = pi x r x r, where r is the radius and pi is about 3.14159. This program uses the exact value of pi from the math module and also prints the circumference, 2 x pi x r.
Program
area_of_circle.py
import math
radius = 7 # in cm; or: radius = float(input("Enter the radius: "))
area = math.pi * radius ** 2
circumference = 2 * math.pi * radius
print("Radius:", radius, "cm")
print(f"Area: {area:.2f} square cm")
print(f"Circumference: {circumference:.2f} cm")
Output
Radius: 7 cm Area: 153.94 square cm Circumference: 43.98 cm
How it works
import mathgives access tomath.pi, which holds pi to 15 decimal places.radius ** 2squares the radius, and multiplying bymath.pigives the area.2 * math.pi * radiusgives the circumference, the distance around the circle.:.2frounds both results to two decimal places.
Another way to write it
Using 22/7 for pi
Using 22/7 for pi
radius = 7 # cm
area = 22 / 7 * radius ** 2
print(f"Area of the circle: {area:.2f} square cm")
Output
Area of the circle: 154.00 square cm
Many textbooks use 22/7 as an approximation of pi. With a radius of 7 it gives a neat 154, slightly more than the exact 153.94.
Try it yourself
Change the program to find the radius of a circle whose area is 100 square cm, using math.sqrt().
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises