In this example, you will create python program to calculate area of circle with differant methods like using user define function, using python math module.
This program will first ask user to input radius using input() function and store it into variable and define constant value of PI.
Now, it will calculate area of circle using the formula Area = PI * r * r.
And at last, it will print area of circle to output.
Example :
PI = 3.14
r = float(input("Enter the radius of a circle:"))
area = PI * r * r
print("Area of a circle = %.2f" %area)
Output :
Enter the radius of a circle:10
Area of a circle = 314.00
Here, we will see python program to find the area of a circle using function. We will define Area() function which take one argument and return area of circle.
Example :
def Area(r):
PI = 3.14
return PI * (r*r);
r = float(input("Enter the radius of a circle:"))
print("Area of circle = %.2f" % Area(r));
Output :
Enter the radius of a circle:8.13
Area of circle = 207.544266
Python math module contains some of the popular mathematical functions. These include trigonometric functions, representation functions, logarithmic functions, angle conversion functions, etc.
Before using math module, we need to import it into our program.
Example :
import math
r = float(input("Enter the radius of a circle:"))
area = math.pi * r * r
print("Area of a circle = %.2f" %area)
Output :
Enter the radius of a circle:125
Area of a circle = 49087.39
Here, math.pi returns value of PI.
Ask anything about this examples