Write a Python Program to Find the Area of a Circle

In this Python tutorial, I will show you how to write a Python program to find the area of a circle.

While building the advanced calculator using Python, I had to add the functionality of computing the area of a circle.

For that, I used the concepts of function with the math module to create that functionality. In this tutorial, I have explained how you can create the same function to implement the functionality of calculating the area of a circle.

Additionally, I have explained two more methods for calculating the area of a circle.

Write a Python Program to Find the Area of the Circle

To find the area of the circle, you must know about the formula for computing the area of the circle. The formula is area = PI * r * r. Where the value of PI is 3.14 and r is the radius of the circle.

To write a Python program to find the area of a circle, there are multiple approaches that are explained below.

Write a Program to Print the Area of a Circle in Python

Here, I will show you a simple or naive method for computing the area of a circle. Just execute the code below, which accepts the radius of the circle from the user as input and returns the area of the circle as output.

PI = 3.14
r = float(input("Enter the radius of a circle:"))
area = PI * r * r
print("Area of a circle = %.2f" %area)
Write a Program to Print Area of Circle in Python

Look at the output, which shows that the area of a circle with a radius of 10 is 314.00.

READ:  Python Dictionary Update

Let’s understand the code part. First, we define the constant PI with the value 3.14 using PI = 3.14. Then, we accept the radius of the circle from the user using ‘r = float(input(“Enter the radius of a circle:”))’.

The input() function allows you to give input through the terminal. This input is then converted into a float or decimal value using the float() function. Finally, it is stored in the variable named ‘r’, which contains the radius of the circle.

Then, computing the area of the circle using the formula defined as ‘area = PI * r * r’, as a result, it returns the area of a circle as 314.00.

Similarly, you can calculate the area of a circle with different radius values. This is how to write a program to print the area of a circle in Python.

Program to Calculate Area of Circle in Python using Custom Function

In the above section you have created program to calculate the the area of circle, but that code is very simple, which means suppose you need to reuse that code in your project multiple times, are you going to write the same code again and again?. I don’t think so, if you write that it is very cumbersome.

Here is the simple solution: create a function containing the code to compute the area of a circle and call this function wherever you need it. Run or execute the code below to create a function named circleArea.

def circleArea(r):
    PI = 3.14
    return PI * (r*r);

Now, your function is ready. To calculate the area of a circle, just call this function and provide the radius of the circle, as shown below.

print("Area of circle = %.6f" % circleArea(6));
print("Area of circle = %.6f" % circleArea(15));
Program to Calculate Area of Circle in Python using Custom Function

From the output, call the function two times with different radiuses, like circleArea(6) and circleArea(15), which returns the area of the two circles, which are 113.04 and 706.500, respectively.

READ:  Complex Numbers in Python

So, you wrapped your code into a function named circleArea, which accepts one parameter: the radius of the circle.

This is how to write a program to calculate the area of a circle using the custom function.

Program to Calculate Area of Circle in Python using Math Module

The math module of Python has a wide range of functions related to mathematics operations, so here you can use this module. However, it does not have any function to directly calculate the area of a circle. Instead, you can use the const PI from this module.

So that means you don’t need to define the value of PI, you can access it from the math module.

Now, use the code below to create a program to calculate the area of a circle using the math module.

import math
def circleArea(r):
    return math.pi * (r*r);

Let’s test this function which contains the PI value from the math module.

print("Area of circle = %.2f" % circleArea(30));
Program to Calculate Area of Circle in Python using math Module

So, you can see in the output that it outputs the area of a circle with a radius of 30 as 2827.43.

Here, instead of a hard-coded PI constant value, the PI value is accessed from the math module using math.pi.

This is how to write a program to calculate the area of a circle using the Python math module.

Conclusion

In this Python tutorial, you learned how to write a Python program to find the area of a circle.

You used the simple approach, customer function and the math module to calculate the area of the circle.

You may like to read: