Task1 BasicProgramming AbelHendrikMP TI23T
Task1 BasicProgramming AbelHendrikMP TI23T
Description Representation:
1. Start
2. Input the base length of the triangle.
3. Input the height of the triangle.
4. Calculate the area of the triangle using the formula: Area = 0.5 * base *
height
5. Output the area of the triangle.
6. End
Flowchart:
Start
|
v
Input base
|
v
Input height
|
v
Calculate Area = 0.5 * base * height
|
v
Output Area
|
v
End
Pseudocode:
START
INPUT base
INPUT height
SET Area = 0.5 * base * height
OUTPUT Area
END
This algorithm will calculate the area of a triangle given the base and height. Please note
that this formula is applicable for right-angled triangles and for other triangles where the
height is known.
Task 1 Programming Basic
With heron’s formula when lenght of 3 sides are know instead of height of triangle
Description Representation:
1. Start
2. Input the lengths of the three sides of the triangle (a, b, c).
3. Calculate the semi-perimeter of the triangle using the formula: s = (a + b + c)
/ 2
4. Calculate the area of the triangle using Heron’s formula: Area = sqrt(s * (s -
a) * (s - b) * (s - c))
5. Output the area of the triangle.
6. End
Flowchart:
Start
|
v
Input side a
|
v
Input side b
|
v
Input side c
|
v
Calculate s = (a + b + c) / 2
|
v
Calculate Area = sqrt(s * (s - a) * (s - b) * (s - c))
|
v
Output Area
|
v
End
Pseudocode:
START
INPUT side a
INPUT side b
INPUT side c
SET s = (a + b + c) / 2
SET Area = sqrt(s * (s - a) * (s - b) * (s - c))
OUTPUT Area
END
This algorithm will calculate the area of a triangle given the lengths of all three sides using
Heron’s formula. This formula is applicable for all types of triangles.
Task 1 Programming Basic
Python code :
1. Calculating the area of a triangle when the base and height are known:
def calculate_area_base_height():
base = float(input( "Enter the base of the triangle: " ))
height = float(input( "Enter the height of the triangle: " ))
area = 0.5 * base * height
print( "The area of the triangle is: ", area )
calculate_area_base_height()
2. Calculating the area of a triangle using Heron’s formula when all three sides
are known:
import math
def calculate_area_herons_formula():
a = float(input( "Enter side a: " ))
b = float(input( "Enter side b: " ))
c = float(input( "Enter side c: " ))
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print( "The area of the triangle is: " , area)
calculate_area_herons_formula()
In both of these Python functions, we first take the necessary inputs, perform the
calculations, and then print out the result.