In this post, we will write a program for how to calculate circumference of a circle in python.
First, let’s see What is Circumference of Circle?
The perimeter is the curve length around any closed figure.
Formula:
Circumference of Circle: C = 2 * pi * r Where, r = Radius of circle pi = pi is geek letter which is a constant whose approximate value is 3.14159.. or 22/7 .
Examples:
Input: Radius = 5
Output: The circumference of the circle is 31.40.Input: Radius = 2
Output: The circumference 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 circumference of a circle.
# define a function for # calculating circumference # of a circle def circumference_circle(r): # constant value PI = 3.14 # printing the radius print("Radius =",r) # calculating the circumference C = 2 * PI * r # printing the circumference print("The circumference of the circle is", C) # main code if __name__ == "__main__" : # input radius radius = 5 # function calling circumference_circle(radius)
Output:
Radius = 5 The circumference of the circle is 31.400000000000002
Code Version 2: Create a user-defined function that will calculate and print the circumference 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 # define a function for # calculating circumference # of a circle def circumference_circle(r): # printing the radius print("Radius =",r) # calculating the circumference C = 2 * pi * r # printing the circumference print("The circumference of the circle is", C) # main code if __name__ == "__main__" : # input radius radius = 2 # function calling circumference_circle(radius)
Output:
Radius = 2 The circumference of the circle is 12.566370614359172
Code version 3: Create a user-defined one-line anonymous function which will calculate and return the circumference of a circle using a lambda statement.
# including pi constant # from the math library # into this program from math import pi # main code if __name__ == "__main__" : # input radius radius = 2 # printing the radius print("Radius =", radius) # create a one line function # using lambda statement # for calculating # circumference of a # circle circumference_circle = lambda r : 2 * pi * r # function calling print("The circumference of the circle is", circumference_circle(radius))
Output:
Radius = 2 The circumference of the circle is 12.566370614359172