The Wayback Machine - https://web.archive.org/web/20240930164914/https://www.geeksforgeeks.org/c-program-find-area-triangle/
Open In App

Program to find area of a triangle

Last Updated : 16 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Show Topics
Solve Problem
Basic
23.8%
8.4K

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
Recommended Practice

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

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++ Program to find the area 
// of triangle 
#include <bits/stdc++.h>
using namespace std;
  
float findArea(float a, float b, float c) 
{ 
    // Length of sides must be positive 
    // and sum of any two sides 
    // must be smaller than third side. 
    if (a < 0 || b < 0 || c < 0 || 
       (a + b <= c) || a + c <= b || 
                       b + c <= a) 
    { 
        cout << "Not a valid triangle"; 
        exit(0); 
    } 
    float s = (a + b + c) / 2; 
    return sqrt(s * (s - a) * 
                    (s - b) * (s - c)); 
} 
  
// Driver Code
int main() 
{ 
    float a = 3.0; 
    float b = 4.0; 
    float c = 5.0; 
  
    cout << "Area is " << findArea(a, b, c); 
    return 0; 
} 
  
// This code is contributed 
// by rathbhupendra

                    

C

#include <stdio.h> 
#include <stdlib.h> 
  
float findArea(float a, float b, float c)
{
    // Length of sides must be positive and sum of any two sides
    // must be smaller than third side.
    if (a < 0 || b < 0 || c <0 || (a+b <= c) ||
        a+c <=b || b+c <=a)
    {
        printf("Not a valid triangle");
        exit(0);
    }
    float s = (a+b+c)/2;
    return sqrt(s*(s-a)*(s-b)*(s-c));
}
  
int main()
{
    float a = 3.0;
    float b = 4.0;
    float c = 5.0;
  
    printf("Area is %f", findArea(a, b, c));
    return 0;
}

                    

Java

// Java program to print
// Floyd's triangle
      
class Test
{
    static float findArea(float a, float b, float c)
    {
        // Length of sides must be positive and sum of any two sides
        // must be smaller than third side.
        if (a < 0 || b < 0 || c <0 || (a+b <= c) ||
            a+c <=b || b+c <=a)
        {
            System.out.println("Not a valid triangle");
            System.exit(0);
        }
        float s = (a+b+c)/2;
        return (float)Math.sqrt(s*(s-a)*(s-b)*(s-c));
    }
          
    // Driver method
    public static void main(String[] args) 
    {
        float a = 3.0f;
        float b = 4.0f;
        float c = 5.0f;
      
        System.out.println("Area is " + findArea(a, b, c));
    }
}

                    

Python3

# Python Program to find the area 
# of triangle 
  
# Length of sides must be positive 
# and sum of any two sides 
def findArea(a,b,c): 
  
    # must be smaller than third side. 
    if (a < 0 or b < 0 or c < 0 or (a+b <= c) or (a+c <=b) or (b+c <=a) ): 
        print('Not a valid triangle') 
        return
          
    # calculate the semi-perimeter 
    s = (a + b + c) / 2
      
    # calculate the area 
    area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
    print('Area of a triangle is %f' %area) 
  
  
# Initialize first side of triangle 
a = 3.0
# Initialize second side of triangle 
b = 4.0
# Initialize Third side of triangle 
c = 5.0
findArea(a,b,c) 
  
# This code is contributed by Shariq Raza 

                    

C#

// C# program to print
// Floyd's triangle
using System;
  
class Test {
      
    // Function to find area
    static float findArea(float a, float b,
                        float c)
    {
          
        // Length of sides must be positive
        // and sum of any two sides
        // must be smaller than third side.
        if (a < 0 || b < 0 || c <0 || 
        (a + b <= c) || a + c <=b || 
            b + c <=a)
        {
            Console.Write("Not a valid triangle");
            System.Environment.Exit(0);
        }
        float s = (a + b + c) / 2;
        return (float)Math.Sqrt(s * (s - a) * 
                            (s - b) * (s - c));
    }
          
    // Driver code
    public static void Main() 
    {
        float a = 3.0f;
        float b = 4.0f;
        float c = 5.0f;
      
        Console.Write("Area is " + findArea(a, b, c));
    }
}
  
// This code is contributed Nitin Mittal.

                    

PHP

<?php
function findArea($a, $b, $c)
{
    // Length of sides must be positive
    // and sum of any two sides must
    // be smaller than third side.
    if ($a < 0 or $b < 0 or
        $c < 0 or ($a + $b <= $c) or
        $a + $c <= $b or $b + $c <= $a)
    {
        echo "Not a valid triangle";
        exit(0);
    }
    $s = ($a + $b + $c) / 2;
    return sqrt($s * ($s - $a) * 
            ($s - $b) * ($s - $c));
}
  
// Driver Code
$a = 3.0;
$b = 4.0;
$c = 5.0;
  
echo "Area is ", findArea($a, $b, $c);
  
// This code is contributed anuJ_67.
?>

                    

Javascript

<script>
  
// javascript Program to find the area 
// of triangle 
  
function findArea( a,  b,  c) 
{ 
    // Length of sides must be positive 
    // and sum of any two sides 
    // must be smaller than third side. 
    if (a < 0 || b < 0 || c < 0 || 
       (a + b <= c) || a + c <= b || 
                       b + c <= a) 
    { 
        document.write( "Not a valid triangle"); 
        return;
    } 
    let s = (a + b + c) / 2; 
    return Math.sqrt(s * (s - a) * 
                    (s - b) * (s - c)); 
} 
  
// Driver Code
  
    let a = 3.0; 
    let b = 4.0; 
    let c = 5.0; 
  
  document.write( "Area is " + findArea(a, b, c)); 
  
// This code is contributed by todaysgaurav 
  
</script>

                    

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 = \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}

= \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++

// C++ program to evaluate area of a polygon using
// shoelace formula
#include <bits/stdc++.h>
using namespace std;
   
// (X[i], Y[i]) are coordinates of i'th point.
double polygonArea(double X[], double Y[], int n)
{
    // Initialize area
    double area = 0.0;
   
    // Calculate value of shoelace formula
    int j = n - 1;
    for (int i = 0; i < n; i++)
    {
        area += (X[j] + X[i]) * (Y[j] - Y[i]);
        j = i;  // j is previous vertex to i
    }
   
    // Return absolute value
    return abs(area / 2.0);
}
   
// Driver program to test above function
int main()
{
    double X[] = {0, 2, 4};
    double Y[] = {1, 3, 7};
   
    int n = sizeof(X)/sizeof(X[0]);
   
    cout << polygonArea(X, Y, n);
}

                    

Java

// Java program to evaluate area of 
// a polygon usingshoelace formula
import java.io.*;
import java.math.*;
  
class GFG {
  
    // (X[i], Y[i]) are coordinates of i'th point.
    static double polygonArea(double X[], double Y[], int n)
    {
        // Initialize area
        double area = 0.0;
      
        // Calculate value of shoelace formula
        int j = n - 1;
        for (int i = 0; i < n; i++)
        {
            area += (X[j] + X[i]) * (Y[j] - Y[i]);
              
            // j is previous vertex to i
            j = i; 
        }
      
        // Return absolute value
        return Math.abs(area / 2.0);
    }
      
    // Driver program 
    public static void main (String[] args) 
    {
        double X[] = {0, 2, 4};
        double Y[] = {1, 3, 7};
  
        int n = X.length;
        System.out.println(polygonArea(X, Y, n));
    }
}
  
  
// This code is contributed
// by Nikita Tiwari.

                    

Python3

# Python 3 program to evaluate
# area of a polygon using
# shoelace formula
  
# (X[i], Y[i]) are coordinates of i'th point.
def polygonArea(X,Y, n) :
  
    # Initialize area
    area = 0.0
    
    # Calculate value of shoelace formula
    j = n - 1
    for i in range( 0, n) :
        area = area + (X[j] + X[i]) * (Y[j] - Y[i])
        j = i  # j is previous vertex to i
      
      
    # Return absolute value
    return abs(area // 2.0)
  
    
# Driver program to test above function
X = [0, 2, 4]
Y = [1, 3, 7]
  
n = len(X)
print(polygonArea(X, Y, n))
  
  
# This code is contributed
# by Nikita Tiwari.

                    

C#

// C# program to evaluate area of 
// a polygon usingshoelace formula
using System;
  
class GFG {
  
    // (X[i], Y[i]) are coordinates 
    // of i'th point.
    static double polygonArea(double []X,
                       double []Y, int n)
    {
        // Initialize area
        double area = 0.0;
      
        // Calculate value of shoelace
        // formula
        int j = n - 1;
        for (int i = 0; i < n; i++)
        {
            area += (X[j] + X[i]) * 
                        (Y[j] - Y[i]);
              
            // j is previous vertex to i
            j = i; 
        }
      
        // Return absolute value
        return Math.Abs(area / 2.0);
    }
      
    // Driver program 
    public static void Main () 
    {
        double []X = {0, 2, 4};
        double []Y = {1, 3, 7};
  
        int n = X.Length;
        Console.WriteLine(
                 polygonArea(X, Y, n));
    }
}
  
// This code is contributed by anuj_67.

                    

PHP

<?php
// PHP program to evaluate area of a 
// polygon using shoelace formula
  
// (X[i], Y[i]) are coordinates
// of i'th point.
function polygonArea( $X, $Y, $n)
{
      
    // Initialize area
    $area = 0.0;
  
    // Calculate value of 
    // shoelace formula
    $j = $n - 1;
    for ( $i = 0; $i < $n; $i++)
    {
        $area += ($X[$j] + $X[$i]) * 
                 ($Y[$j] - $Y[$i]);
                   
        // j is previous vertex to i
        $j = $i; 
    }
  
    // Return absolute value
    return abs($area / 2.0);
}
  
    // Driver Code
    $X = array(0, 2, 4);
    $Y = array(1, 3, 7);
    $n = count($X);
    echo polygonArea($X, $Y, $n);
  
// This code is contributed by anuj_67.
?>

                    

Javascript

<script>
  
// Javascript program to evaluate area of a polygon using
// shoelace formula
  
// (X[i], Y[i]) are coordinates of i'th point.
function polygonArea(X, Y, n)
{
  
    // Initialize area
    let area = 0.0;
  
    // Calculate value of shoelace formula
    let j = n - 1;
    for (let i = 0; i < n; i++)
    {
        area += (X[j] + X[i]) * (Y[j] - Y[i]);
        j = i; // j is previous vertex to i
    }
  
    // Return absolute value
    return Math.abs(area / 2.0);
}
  
// Driver program to test above function
    let X = [0, 2, 4];
    let Y = [1, 3, 7];
  
    let n = X.length;
  
    document.write(polygonArea(X, Y, n));
  
// This code is contributed by Mayank Tyagi
  
</script>

                    

Output
2

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



Previous Article
Next Article

Similar Reads

Calculate ratio of area of a triangle inscribed in an Ellipse and the triangle formed by corresponding points on auxiliary circle
Given two integers A and B representing the lengths of semi-major and semi-minor axes of an ellipse, the task is to calculate the ratio of any triangle inscribed in the ellipse and that of the triangle formed by the corresponding points on its auxiliary circle. Examples: Input: A = 1, B = 2Output: 2Explanation: Ratio = B / A = 2 / 1 = 2 Input: A =
4 min read
Program to find Area of Triangle inscribed in N-sided Regular Polygon
Given the triangle inscribed in an N-sided regular polygon with given side length, formed using any 3 vertices of the polygon, the task is to find the area of this triangle. Examples: Input: N = 6, side = 10 Output: 129.904 Input: N = 8, side = 5 Output: 45.2665 Approach: Consider the 1st example: Given is a 6 sided regular polygon ABCDEF with a tr
6 min read
Program to print a Hollow Triangle inside a Triangle
Given a number N(? 8), the task is to print a Hollow Triangle inside a Triangle pattern.Example: Input: N = 9 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Approach: Let i be the index for rows and j be the index for columns. Then: For sides of outer triangle: If the index of column(j) is equals to (N - i + 1) or (
7 min read
Program to calculate area and perimeter of equilateral triangle
An equilateral triangle is a triangle in which all three sides and angles are equal. All three internal angles of equilateral triangle measures 60 degree. If we know the length of each sides of equilateral triangle, then we can use below mentioned formula to calculate area of equilateral triangle.Area of Equilateral Triangle = (sqrt(3)/4) * a * a I
4 min read
Program to calculate area of Circumcircle of an Equilateral Triangle
Given the length of sides of an equilateral triangle. We need to write a program to find the area of Circumcircle of the given equilateral triangle.Examples: Input : side = 6 Output : Area of circumscribed circle is: 37.69 Input : side = 9 Output : Area of circumscribed circle is: 84.82 All three sides of equilateral triangle are of equal length an
4 min read
Program to calculate the Area and Perimeter of Incircle of an Equilateral Triangle
Given the length of sides of an equilateral triangle, the task is to find the area and perimeter of Incircle of the given equilateral triangle. Examples: Input: side = 6 Output: Area = 9.4. Perimeter = 10.88 Input: side = 9 Output: Area = 21.21, Perimeter = 16.32 Properties of an Incircle are: The center of the Incircle is same as the center of the
4 min read
Find area of triangle if two vectors of two adjacent sides are given
Given two vectors in form of (xi+yj+zk) of two adjacent sides of a triangle. The task is to find out the area of a triangle.Examples: Input: x1 = -2, y1 = 0, z1 = -5 x2 = 1, y2 = -2, z2 = -1 Output: Area = 6.422616289332565Input: x1 = -2, y1 = 1, z1 = 5 x2 = 1, y2 = 3, z2 = -1 Output: Area = 8.860022573334675 Approach: Suppose we have two vectors a
5 min read
Find the altitude and area of an isosceles triangle
Given the side (a) of the isosceles triangle. The task is to find the area (A) and the altitude (h). An isosceles triangle is a triangle with 2 sides of equal length and 2 equal internal angles adjacent to each equal sides. In this figure, a- Measure of the equal sides of an isosceles triangle. b- Base of the isosceles triangle. h- Altitude of the
4 min read
Find the height of a right-angled triangle whose area is X times its base
Given that the area of a right-angled triangle is X times its base b. The task is to find the height of the given triangle. Examples: Input: X = 40 Output: 80Input: X = 100 Output: 200 Approach: We know that the area of a right-angled triangle, Area = (base * height) / 2 and it is given that this area is X times the base i.e. base * X = (base * hei
3 min read
Find the coordinates of a triangle whose Area = (S / 2)
Given an integer S, the task is to find the coordinates of a triangle whose area is (S / 2). Examples: Input: S = 4 Output: (0, 0) (1000000000, 1) (999999996, 1) Input: S = 15 Output: (0, 0) (1000000000, 1) (999999985, 1) Approach: It is known than the area of the triangle whose coordinates are (X1, Y1), (X2, Y2) and (X3, Y3) is given by A = ((X1 *
5 min read
Find all sides of a right angled triangle from given hypotenuse and area | Set 1
Given hypotenuse and area of a right angle triangle, get its base and height and if any triangle with given hypotenuse and area is not possible, print not possible.Examples: Input : hypotenuse = 5, area = 6 Output : base = 3, height = 4 Input : hypotenuse = 5, area = 7 Output : No triangle possible with above specification. Recommended PracticeArea
9 min read
Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle
Given an array arr[] consisting of N integers, the task is to find the size of the largest subset of the array such that a triangle can be formed from any of the three integers of the subset as the sides of a triangle. Examples: Input: arr[] = {1, 4, 7, 4}Output: 3Explanation: A possible subsets that follow the given conditions are {1, 4, 4} and {4
7 min read
Sum of upper triangle and lower triangle
Given a N x M matrix. The task is to print the sum of upper and lower triangular elements (i.e elements on the diagonal and the upper and lower elements). Examples : Input: {{6, 5, 4}, {1, 2, 5}, {7, 9, 7}}Output:Upper sum is 29Lower sum is 32 Input: {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}Output:Upper sum is 10Lower sum is 14 Recommended Problem Sum of u
6 min read
Find area of the larger circle when radius of the smaller circle and difference in the area is given
Given two integers r and d where r is the radius of the smaller circle and d is the difference of the area of this circle with some larger radius circle. The task is to find the area of the larger circle.Examples: Input: r = 4, d = 5 Output: 55.24 Area of the smaller circle = 3.14 * 4 * 4 = 50.24 55.24 - 50.24 = 5Input: r = 12, d = 3 Output: 455.16
4 min read
Find area of the Circle when the area of inscribed Square is given
Given the area of a square inscribed in a circle as N, the task is to calculate the area of a circle in which the square is inscribed. Examples: Input: N = 4 Output: 6.283 Input: N = 10Output: 15.707 Approach: Consider the below image: Let the area of the square is 'A'The side of the square is given by = A**(1/2)A right-angled triangle is formed by
4 min read
Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
Given here is an ellipse with axes length 2a and 2b, which inscribes a rectangle of length l and breadth h, which in turn inscribes a triangle.The task is to find the area of this triangle.Examples: Input: a = 4, b = 3 Output: 12 Input: a = 5, b = 2 Output: 10 Approach: We know the Area of the rectangle inscribed within the ellipse is, Ar = 2ab(Ple
4 min read
Area of the Largest Triangle inscribed in a Hexagon
Given here is a regular hexagon, of side length a, the task is to find the area of the biggest triangle that can be inscribed within it.Examples: Input: a = 6 Output: area = 46.7654 Input: a = 8 Output: area = 83.1384 Approach: It is very clear that the biggest triangle that can be inscribed within the hexagon is an equilateral triangle. In triangl
3 min read
Number of possible pairs of Hypotenuse and Area to form right angled triangle
Given two arrays H and S. The array H[] contains the length of the hypotenuse and the array S[] contains Area of a right-angled triangle. The task is to find all possible pairs of (H, S) such that we can construct a right-angled triangle with hypotenuse H and area S.Examples: Input : H[] = {1, 6, 4} ; S[] = {23, 3, 42, 14} Output : 2 Possible pairs
12 min read
Area of a largest square fit in a right angle triangle
Given a right angled triangle with height l, base b & hypotenuse h.We need to find the area of the largest square that can fit in the right angled triangle. Examples: Input: l = 3, b = 4, h = 5 Output: 2.93878 The biggest square that can fit inside is of 1.71428 * 1.71428 dimension Input: l = 5, b = 12, h = 13 Output: 12.4567 Considering the ab
8 min read
Area of a square inscribed in a circle which is inscribed in an equilateral triangle
Given here is an equilateral triangle with side length a, which inscribes a circle, which in turn inscribes a square. The task is to find the area of this square.Examples: Input: a = 6 Output: 1 Input: a = 10 Output: 0.527046 Approach: let r be the radius of circle, hence it is the inradius of equilateral triangle, so r = a /(2 * ?3) diagonal of sq
4 min read
Minimum increment in the sides required to get non-negative area of a triangle
Given three sides of the triangle, find the minimum increase in the length of the side of the triangle to make the area of the triangle non-negative. Examples: Input: a = 3, b = 4, c = 10 Output: 3 With the given sides, the area is negative. If a is increased to 5 and b to 5, then the area becomes 0, which is not negative. Input: a = 6, b = 6, c =
5 min read
Area of circle which is inscribed in equilateral triangle
Given here is an equilateral triangle with side length a, the task is to find the area of the circle inscribed in that equilateral triangle.Examples: Input : a = 4 Output : 4.1887902047863905 Input : a = 10 Output : 26.1799387799 Approach: Area of equilateral triangle = [Tex] [/Tex]Semi perimeter of equilateral triangle = (a + a + a) / 2Radius of i
3 min read
Area of Circumcircle of a Right Angled Triangle
Given an integer C which is the length of the hypotenuse of a right angled triangle of a circumcircle passing through the centre of the circumcircle. The task is to find the area of the circumcircle. Examples: Input: C = 8 Output: 50.26Input: C = 10 Output: 78.53 Approach: Since the hypotenuse C passes through the center of the circle, the radius o
5 min read
Area of Incircle of a Right Angled Triangle
Given the P, B and H are the perpendicular, base and hypotenuse respectively of a right angled triangle. The task is to find the area of the incircle of radius r as shown below: Examples: Input: P = 3, B = 4, H = 5 Output: 3.14Input: P = 5, B = 12, H = 13 Output: 12.56 Approach: Formula for calculating the inradius of a right angled triangle can be
4 min read
Area of a triangle inside a parallelogram
Given the base and height of the parallelogram ABCD are b and h respectively. The task is to calculate the area of the triangle ▲ABM (M can be any point on upper side) constructed on the base AB of the parallelogram as shown below: Examples: Input: b = 30, h = 40 Output: 600.000000 Approach: Area of a triangle constructed on the base of parallelogr
3 min read
Area of triangle formed by the axes of co-ordinates and a given straight line
Given a straight line with equation coefficients as a, b & c(ax + by + c = 0), the task is to find the area of the triangle formed by the axes of co-ordinates and this straight line.Examples: Input: a = -2, b = 4, c = 3 Output: 0.5625 Input: a = 4, b = 3, c = 12 Output: 6 Approach: Let PQ be the straight line having AB, the line segment between
4 min read
Area of Equilateral triangle inscribed in a Circle of radius R
Given an integer R which denotes the radius of a circle, the task is to find the area of an equilateral triangle inscribed in this circle. Examples: Input: R = 4 Output: 20.784 Explanation: Area of equilateral triangle inscribed in a circle of radius R will be 20.784, whereas side of the triangle will be 6.928 Input: R = 7 Output: 63.651 Explanatio
4 min read
Maximum area of rectangle inscribed in an equilateral triangle
Given an integer A, which denotes the side of an equilateral triangle, the task is to find the maximum area of the rectangle that can be inscribed in the triangle.Examples: Input: A = 10 Output: 21.65 Explanation: Maximum area of rectangle inscribed in an equilateral triangle of side 10 is 21.65.Input: A = 12 Output: 31.176 Explanation: Maximum are
4 min read
Area of Circumcircle of an Equilateral Triangle using Median
Given the median of the Equilateral triangle M, the task is to find the area of the circumcircle of this equilateral triangle using the median M. Examples: Input: M = 3 Output: 12.5664 Input: M = 6 Output: 50.2655 Approach: The key observation in the problem is that the centroid, circumcenter, orthocenter and incenter of an equilateral triangle all
4 min read
Area of a Triangle from the given lengths of medians
Given three integers A,B and C which denotes length of the three medians of a triangle, the task is to calculate the area of the triangle. A median of a triangle is a line segment joining a vertex to the midpoint of the opposite side, thus bisecting that side. Examples: Input: A = 9, B = 12, C = 15 Output: 72.0Input: A = 39, B = 42, C = 45 Output:
4 min read