Sunday, June 1, 2014

Area of a Circle in Python


Beginners, students and hobbyist who spend their time in learning how to write simple program may encounter solving the problem of area of a circle by finding its radius. In this article I will show you how to write you own program in solving the area of the circle by using Python as your programming language. In developing this program I was able to identify what is the program should suppose to do. 

First our program will ask our user to enter the radius of our circle. After the user provided the radius of our circle our program will perform a series of computation to find the area of the circle. In this example of mine I have created a function to return the computed value of the area of the circle. I called the function area_of_circle(r) with a parameter r it represent the radius of our circle. The formula we use to find and solve the area of the circle is  a = r**2 * math.pi. The command math.pi is a built in function i Python which contains the fixed value of a pi of a circle that has a value of  3.141592653589793. In order for us to use the math.pi statement we must include this library file command on the top of our program with this command import math this instruction tells our Python compiler to include the math library file in our program so that we can use properly the math.pi statement in our program.

def area_of_circle(r):
    a = r**2 * math.pi
    return a

A function to solve the area of the circle 

In addition my program ask the user whether the user will continue to us the program of not. It enables our user to run the program again and give a another different value of area of the circle. By doing so it makes our program interactive, user friendly and more easier to us.

I hope you find my work useful in learning how to write a program using Python.

Thank you very much.


Sample Output Of Our Program


Python Text Editor where are program is written


Program Listing

# area of a circle solver
# Written By: Mr. Jake R. Pomperada, MAED-IT
# Tools : Python
# Date  : June 1, 2014

import math

def area_of_circle(r):
    a = r**2 * math.pi
   return a

print("\n")
print("@==============================@")
print("    Area of a Cirlce Solver     ")
print("@==============================@")

complete = False
while complete == False:
 print("");
 radius = int(input("What is the radius of the circle :=> "))
 print("");
 print ("The area of the circle is ",format(round(area_of_circle(radius),4)),'.')
 print("")

 choice = input("Are You Finish?  Y/N: ").lower().strip()

 if choice == "y":
      complete = True
 else:
      print("")

print("\n")
print("Thank You For Using This Program.")
print("\n")       


No comments:

Post a Comment