codescracker
python

Python Program to Calculate Circumference of Circle



« Previous Program Next Program »

In this article, we've created some programs in Python, to find and print circumference value of a circle based on the radius value entered by user at run-time. Here are the list of programs:

Before creating these programs, let me remind you about the formula used here.

Formula to Find Circumference of Circle

To calculate circumference of a circle based on its radius, use following formula:

circum = 2πr

Here circum indicates to circumference value, π indicates to 3.14 and r indicates to the radius of circle.

Calculate Circumference of Circle

To calculate circumference of a circle in Python, you have to ask from user to enter the radius, and then find and print circumference as shown in the program given below. The question is, write a Python program to receive radius from user and print circumference of circle. Here is its answer:

print("Enter Radius of Circle: ")
r = float(input())
c = 3.14*r*r
print("\nCircumference = ", c)

Here is the initial output produced by this program:

calculate circumference of circle python

Now supply the radius value of circle say 4 and press ENTER key to find and print the circumference as shown in the snapshot given below:

circumference of circle python

Find Circumference of Circle using Function

This program uses a user-defined function named findCircum() to find and return circumference value based on the value (radius) passed as its argument.

def findCircum(rad):
    return 3.14*rad*rad

print("Enter Radius of Circle: ", end="")
r = float(input())

c = findCircum(r)
print("\nCircumference = {:.2f}".format(c))

Here is its sample run with user input 2.4:

python calculate circumference of circle

The {:.2f} is used to format the value of c upto two decimal places only, using format() method.

Find Circumference of Circle using Class

This is the last program on finding circumference of a circle, using class and object, an object-oriented feature of Python.

class CodesCracker:
    def findCircum(self, rad):
        return 3.14*rad*rad

print("Enter Radius of Circle: ", end="")
r = float(input())

ob = CodesCracker()
c = ob.findCircum(r)
print("\nCircumference = {:.2f}".format(c))

This program produces the same output as of previous program. In above program, an object named ob is created of CodesCracker class. Therefore, this object can be used to call any member function (findCircum()) of the same class using dot (.) operator.

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker