Hi in this article you will learn a Python program to calculate the area of a triangle as well as area of a circle and print the result to standard output.
In order to find the area of a circle you should remember the formula that is area of a Triangle is denoted by the below formula
Area of Triangle
==================
triangle = ( h * b ) / 2
Where its height of a triangle and, b base of a Triangle
Area of Circle
=======================
circle = pi * (r * r)
Where pi is a constant value, and r is the radius of a circle
Let us use the above two formulas to find the area of a triangle as well as area of a circle by accepting the required inputs from the user.
Design a Python Program to Calculate Area of Triangle And Circle And Print The Result
We require a constant named pi inorder to calculate the area of a circle. Let's import the pi value from the math module.
from math import pi
Take variables height and base, using the input function call ask the user to enter the base and height of the triangle and then convert it into floating point value.
Now take another variable to calculate the area of a triangle and use the above formula. Multiply height with the base and divide it by 2
height = float(input("Enter Height of Triangle : "))
base = float(input("Enter Base of Triangle : "))
area_of_triangle = (height * base) / 2
After calculating the area of a triangle, take another variable named as radius and use the input function call as the user to enter the radius of the circle and convert it into floating point value.
Use the pi value that we had already imported above and calculate the area of a circle by multiplying it with the radius.
radius = float(input("Enter Radius of Circle : "))
area_of_circle = pi * (radius * radius)
Once we calculate the area of a circle and area of a triangle, use the print function call to print the area of a circle and triangle to standard output.
print(f"Area of Triangle is : {area_of_triangle}")
print(f"Area of Circle is : {area_of_circle}")
Complete Python Program
======================
from math import pi
height = float(input("Enter Height of Triangle : "))
base = float(input("Enter Base of Triangle : "))
area_of_triangle = (height * base) / 2
radius = float(input("Enter Radius of Circle : "))
area_of_circle = pi * (radius * radius)
print(f"Area of Triangle is : {area_of_triangle}")
print(f"Area of Circle is : {area_of_circle}")
Output
===============
Enter Height of Triangle : 2.5
Enter Base of Triangle : 3.6
Enter Radius of Circle : 3
Area of Triangle is : 4.5
Area of Circle is : 28.274333882308138
Output
==============
Enter Height of Triangle : 4.9
Enter Base of Triangle : 2.01
Enter Radius of Circle : 3.45
Area of Triangle is : 4.9245
Area of Circle is : 37.392806559352515
Conclusion
==================
The above Python program uses multiple variables to store the required values to calculate the area of triangle and circle. Finally uses the above mentioned formula to calculate the area of a triangle and circle, prints the result to standard output.
Execute the above Python program by providing random integer or floating point values.
Comment it down below if you have any queries regarding the above Python program.
0 Comments