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.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

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.

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);
}

Output

Now let’s have a look at a C++ program to find area of a triangle using its coordinates.

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.

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;
}

Output

Now let’s have a look at a C++ program to find area of a triangle using its base and height.

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;
}

Output

The Java program for the above algo is as follows.

Java

class Area {
static double area(double base, double height) {
return (base * height) / 2;
}
public static void main(String[] args) {
double base = 5, height = 8;
System.out.println("Area of the triangle: " + area(base, height));
}
}

Output

Now, let’s see algo’s implementation in JavaScript language.

JavaScript

let base = 5, height=8;
// Calculating the area
const area = (base * height) / 2;
console.log(
`The area of the triangle is ${area}`
);

Output

The Python implementation of above algo is as follows.

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.

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;
}

Output

Now let’s have a look at C++ program to find area of a triangle using heron's formula.

C++

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float side_a = 4, side_b = 7, side_c = 8;
float area;
float s = (side_a + side_b + side_c) / 2;
area = sqrt(s * (s - side_a) * (s - side_b) * (s - side_c));
cout<<"Area of Triangle= "<<area<<endl;
return 0;
}

Output

The Java program for the above algo is as follows.

Java

public class Demo {
public static void main(String[] args) {
// Sides of a triangle
double s1, s2, s3;
double area, resArea;
// Three sides of a triangle
s1 = 4;
s2 = 7;
s3 = 8;
area = (s1+s2+s3)/2.0d;
resArea = Math.sqrt(area* (area - s1) * (area - s2) * (area - s3));
System.out.println("Area of Triangle = " + resArea);
}
}

Output

Now, let’s see algo’s implementation in JavaScript language.

The Python implementation of the above algo is as follows.

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)

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).

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 60^{o}. 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.

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;
}

Output

Now let’s have a look at a C++ program to find area of a triangle.

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;
}

Output

The Java program for the above algo is as follows.

Java

class Area {
static double area(double side) {
return (1.73 * side * side) / 4;
}
public static void main(String[] args) {
double side = 8;
System.out.println("Area of the equilateral triangle: " + area(side));
}
}

Output

Note: We had used 1.73 in place of sqrt(3).

Now, let’s see algo’s implementation in JavaScript language.

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)

Output

Note: We had used 1.73 in place of sqrt(3).

The Python implementation of the above algo is as follows.

Python

side= 8
area = ( 1.73 * side * side) / 4
print("Area of Equilateral Triangle is: ");
print(area);

Output

Note: We had used 1.73 in place of sqrt(3).

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).

Note: Use of semicolons in python is optional. It doesn't terminate sentences. Also, it doesn't show any error while compiling.

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.

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: