Table of Contents

Compute Area and Circumference of a Circle

Example Description
Learn how to use a Python function to calculate the area and circumference of a circle by entering its radius.

Introduction:

This code defines a Python function that calculates the area and circumference of a circle. It takes the radius as input and returns a tuple containing the area and circumference. The user is prompted to enter the radius and the results are displayed on the console.

Code:

#Function to compute area and circumference of the circle.
def circle(r):
 area = 3.14*r*r
 circumference = 2*3.14*r
 #returns a tuple having two elements area and circumference
 return (area,circumference) 
#end of function
radius = int(input('Enter radius of circle: '))
area,circumference = circle(radius)
print('Area of circle is:',area)
print('Circumference of circle is:',circumference)

Logic:

  1. Define the function “circle” that takes the radius as an argument.
  2. Calculate the area using the formula: area = 3.14 * r * r.
  3. Calculate the circumference using the formula: circumference = 2 * 3.14 * r.
  4. Return a tuple containing the area and circumference.
  5. Prompt the user to enter the radius.
  6. Call the function circle with the entered radius and store the returned values in the variables area and circumference.
  7. Print the calculated area and circumference on the console.

Output:

Enter radius of circle: 4

>> Area of circle is: 50.24

>> Circumference of circle is: 25.12