In this post, we will write a program for how to calculate the area of a circle in python.
First, let’s see What is the Area of Circle?
The area enclosed by a circle of radius r in 2-dimensional plane.
Formula:
Area of circle: A = pi * r * r Or pi * r 2 Where, r = Radius of circle pi = pi is geek letter which is a constant whose approximate value is 3.14159...
Examples:
Input: Radius = 7
Output: The area of the circle is 154.0Input: Radius = 2
Output: The area of the circle is 12.56
Let’s see the program for this:
Code Version 1: Create a user-defined function that will calculate and print the area of a circle.
# define a function for
# finding area of a circle
def area(r):
# constant value of PI
PI = 22/7
# printing the radius
print("Radius =", r)
# calculating the area
area = PI * (r * r)
# printing the area
print("The area of the circle is", area)
# main code
if __name__ == "__main__" :
# input radius
radius = 7
# function calling
area(radius)
Output:
Radius = 7 The area of the circle is 154.0
Code Version 2: Create a user-defined function that will calculate and print the area of a circle. In this code version, we are using pi constant value which is directly taken from the math library.
# including pi constant
# from the math library
# into this program
from math import pi
# defina a function for
# finding area of circle
def area(r):
# printing the radius
print("Radius =", r)
# calculating the area
area = pi * (r * r)
# printing the area
print("The area of the circle is", area)
# main code
if __name__ == "__main__" :
# input radius
radius = 7
# function calling
area(radius)
Output:
Radius = 7 The area of the circle is 153.93804002589985