Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
This article will discuss various approaches to writing a program to find area of a triangle. We will discuss approaches based on the information given to us. We will find the area using the coordinates of vertices, its base and height, and heron’s formula. We will also write a program to find area of a triangle if all the sides are equal, i.e., for an equilateral triangle.
In a two-dimensional plane, the area of a triangle is the region enclosed by it. A triangle is a closed shape with three sides and three vertices. Thus, the area of a triangle is the total space filled by the triangle's three sides.
The general formula for calculating a triangle's area is half the product of its base and height. It is applicable to all forms of triangles, including scalene, isosceles, and equilateral triangles.
Approach
We can write a program to find area of a triangle by using the info provided to us. We can be provided with any of the below-mentioned info.
The coordinates of the vertices of the triangle.
The base and height of the triangle.
The value of each side of the triangle.
The triangle given is an equilateral triangle.
By Using Coordinates of the Vertices of the Triangle
We can write a program to find area of a triangle using coordinates of the vertices. We will have three coordinates of the vertices of the triangle.
We will take a straightforward approach. As we all know, the formula for calculating the area of a triangle with coordinates is as follows:
{ (Ax, Ay), (Bx, By), (Cx, Cy) } is
Area = 1/2 ∣(Ax(By−Cy) + Bx(Cy−Ay) + Cx(Ay−By)∣
Let us now write the program that will convert this logic into code.
Algorithm
We will take the input or assign the values of the coordinates of the triangle.
Further, we will calculate the area using the formula. Area = 1/2 ∣(Ax(By−Cy) + Bx(Cy−Ay) + Cx(Ay−By)∣
Print the area of the triangle.
Implementation
After learning the algorithm to write the program, let’s see its implementation in various programming languages.
Following a C language code for the above algorithm.
Implementation in C
#include <stdio.h>
#include <math.h>
int main(void) {
float Ax,Ay,Bx,By,Cx,Cy;
printf("Enter the value of the coordinates Ax, Ay, Bx, By, Cx, Cy of the triangle:");
scanf("%f %f %f %f %f %f",&Ax, &Ay, &Bx, &By, &Cx, &Cy);
float area = ((float)1 / 2) * abs((int) (Ax * (By - Cy) + Bx * (Cy - Ay) + Cx * (Ay - By)));
printf("AREA OF TRIANGLE IS %f",area);
}
The most basic formula to find the area of a triangle is by using its base and height. We are using this formula from the primary standard. We can also write a program to find the area of triangle by using this formula.
The formula states that the area of a triangle is half of the base value of the triangle multiplied by its height.
Area = (base*height)/2
We can use this formula to frame an algorithm.
Algorithm
We are given the base and height values of the triangle.
Now, we will calculate the area by putting it into the equation. Area = (base*height)/2
Print the area of the triangle.
Implementation
Let us look into the C implementation of the above algorithm.
Implementation in C
#include<stdio.h>
#include <math.h>
int main(){
float base, height, area;
printf("Enter the base and the height of the triangle:");
scanf("%f %f", &base, &height);
area = (base*height) / 2 ;
printf("Area of Triangle is : %f",area);
return 0;
}
Now let’s have a look at a C++ program to find area of a triangle using its base and height.
Implementation in C++
#include<bits/stdc++.h>
using namespace std;
int main(){
float base,height, area;
base= 5;
height= 8;
area = (base*height) / 2 ;
cout<<"Area of Triangle is "<< area << endl;
return 0;
}
You can also try this code with Online C++ Compiler
The Python implementation of above algo is as follows.
Implementation in Python
base = 5
height = 8
area = (base*height)/2
print("Area of Triangle is :");
print(area);
Output
Complexity
The time and space complexity of the above algorithm is as follows.
Time Complexity
The time complexity of the algorithm is O(1).
Space Complexity
The space complexity of the algorithm is O(1).
By Using Heron’s Formula
Heron’s formula is a widely used formula to calculate the area of triangles. Here we are given the three sides of the triangle.
If a, b and c are the three sides of a triangle, respectively, then Heron’s formula is given by:
Let's look at the formula first.
Now let’s see how to use the above formula to write a program to find area of a triangle.
Algorithm
We are provided with the length of the side of the triangle.
We will first find the semi-perimeter of the triangle. S = (Side a + Side b + Side c)/2
Using Heron’s formula, we can calculate the area of the triangle. Area of the triangle = [root( s(a-a)(s-b)(s-c)]
Print the value of the triangle.
Implementation
After learning the algo, write the program to find area of a triangle. Let’s see its implementation in various programming languages.
Below is the C language implementation for the above algo.
Implementation in C
#include <stdio.h>
#include <math.h>
int main()
{
float side_a, side_b, side_c;
printf("Enter the value of the side A, B, C of the triangle:");
scanf("%f %f %f",&side_a, &side_b, &side_c);
float s = (side_a + side_b + side_c) / 2;
// Calculating the area
float area = sqrt(s * (s - side_a) * (s - side_b) * (s - side_c));
printf("The area of the triangle is : %f ", area);
return 0;
}
The Python implementation of the above algo is as follows.
Implementation in Python
side_a = 4;
side_b = 7;
side_c = 8;
# Calculating the semi-perimeter of the triangle
s = (side_a + side_b + side_c)/2;
# Calculating the area of the triangle
area = (s*(s-side_a)*(s-side_b)*(s-side_c)) ** 0.5
print('AREA OF TRIANGLE IS %f' %area)
You can also try this code with Online Python Compiler
The time and space complexity of the above algorithm is as follows.
Time Complexity
The time complexity of the algorithm is O(1).
Space Complexity
The space complexity of the algorithm is O(1).
For an Equilateral Triangle
An equilateral triangle is a type of triangle in which all sides are of the same length. The angles in between them are sure to be 60o. If you are given a triangle whose sides are equal, you can simply use the formula below to find its area.
Area of equilateral triangle = [root(3)*side*side]/4
OR
Area of equilateral triangle = [(1.73)*side*side]/4 (As root(3)~= 1.73)
We can use the approximate value if we do not wish to use the math function in our program.
We can use the above formula to write a program to find area of a triangle. The algorithm of this program is as follows.
Algorithm
We are provided with a side of a triangle. Put it into a float variable.
Calculate the value of the triangle by using the formula Area of equilateral triangle = [root(3)*side*side]/4
You can also use the below formula if you do not wish to use the math function. Area of equilateral triangle = [(1.73)*side*side]/4
Print the value of the triangle.
Implementation
After learning the algo, write the program to find area of a triangle. Let’s see its implementation in various programming languages.
Below is the C language implementation for the above algo.
Implementation in C
#include <stdio.h>
#include <math.h>
int main()
{
float side;
printf("Enter the value of side of the equilateral triangle:");
scanf("%f",&side);
float area = (sqrt(3)/4) * (side * side);
printf("The area of the equilateral Triangle is : %f", area);
return 0;
}
Now let’s have a look at a C++ program to find area of a triangle.
Implementation in C++
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float side=8, area;
area = sqrt(3)/4*(side*side);
cout<<" The area of equilateral triangle is: "<<area<<endl;
cout << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Now, let’s see algo’s implementation in JavaScript language.
Implementation in JavaScript
let i,
side = 8
area = 1
for(i=1;i<=side;i++)
{
area = (1.73*side*side)/4
}
console.log("The area of equilateral triangle is", Math.round(area*100)/100)
You can also try this code with Online Javascript Compiler
Why the complexities of all the above algorithms for finding the area of a triangle are O(1)?
The time complexity of the above programs is O(1). It is because the programs consist of only assignment and arithmetic operations, and all of those will be executed only once. They are also not taking any extra space for storing the value.
What is the most used program to find area of a triangle?
The most commonly used program to find area of a triangle uses the base and height of the given triangle. Given by Area = (base*height)/2.
Is it necessary to include a maths function to do the maths operation?
C++ provides many maths functions that can be used directly in the program. Being a subset of C language, C++ derives most of these mathematical functions from math.h header of C.
What are %c and %s, and why are they used?
The %c and %s are the format specifiers. %c is used to take a character as input or to print a character. In the case of strings, %s is used as the format specifier.
Why are format specifiers used in programming languages?
Format specifiers are used for input and output. The format specifier notion allows the compiler to decide what type of data is in a variable when taking input and publishing it.
What is the Formula of Area of the Triangle?
The formula for the area of a triangle is:
Area=½×Base×Height
Conclusion
This article briefly discussed the program to find area of a triangle. We discussed various approaches based on the info given to us to do it. We hope this blog has helped you write the program to find area of a triangle. If you like to learn more, you can check out our articles: