Open In App

Program to find area of a triangle

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
7 Likes
Like
Report

Given the sides of a triangle, the task is to find the area of this triangle.

Examples : 

Input : a = 5, b = 7, c = 8
Output : Area of a triangle is 17.320508


Input : a = 3, b = 4, c = 5
Output : Area of a triangle is 6.000000

Approach: The area of a triangle can simply be evaluated using following formula. 

Area=(s(sa)(sb)(sc))Area = \sqrt(s*(s-a)*(s-b)*(s-c))

where a, b and c are lengths of sides of triangle, and 
s = (a+b+c)/2

Program to find area of a triangle

Below is the implementation of the above approach:

C++
C Java Python3 C# PHP JavaScript

Output
Area is 6

Time Complexity: O(log2n)
Auxiliary Space: O(1), since no extra space has been taken.

Given the coordinates of the vertices of a triangle, the task is to find the area of this triangle.

Approach: If given coordinates of three corners, we can apply the Shoelace formula for the area below.  

Area=i=1n1(xiy(i+1)+xny1)i=1n1(x(i+1)yix1yn)2Area = \frac{\sum_{i=1}^{n-1} (x_i y_{(i+1)}+x_ny_{1}) - \sum_{i=1}^{n-1}(x_{(i+1)}y_i-x_1y_n)}{2}

=(x1y2+x2y3+...+xn1yn+xny1)(x2y1+x3y2+...+xnyn1+x1yn)2= \frac{(x_{1}y_{2} + x_{2}y_{3} + ... + x_{n-1}y_{n} + x_{n}y_{1}) -(x_{2}y_{1} + x_{3}y_{2} + ... + x_{n}y_{n-1} + x_{1}y_{n})}{2}

C++
Java Python3 C# PHP JavaScript

Output
2

Time Complexity: O(n)
Auxiliary Space: O(1)


Explore