The problem
October 23, 2019 · View on GitHub
Triangle Area
The problem
Take three sides of a triangle. And then calculate the area of the triangle.
How it works
To calculate the area of the triangle. First, calculate the half of the perimeter. Here perimeter is the sum of each side of the triangle.
Let’s call it s.
Then you have to perform square root of the formula like below-
s = (a+b+c)/2
area = √(s(s-a)*(s-b)*(s-c))
the code
import math
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
print('Area of your triangle is ', area)
Explanation
To calculate the square root. We used the math module. And call math.sqrt.
import math
print ( math.sqrt(4) )
This will give you 2 as output.
Similarly, math.sqrt(25) will give 5 as output.
This is something new you have learned this time.
Quiz
How would you calculate the square root of a number.
- Use math.square.root
- Use math.sqroot
- Use math.sqrt
Show Answer
The answer is: 3
take Away
The math module has a lot of math-related functionalities.
tags: programming-hero python python3 problem-solving programming coding-challenge interview learn-python python-tutorial programming-exercises programming-challenges programming-fundamentals programming-contest python-coding-challenges python-problem-solving
