Area of a Triangle

Last Updated : 21 Jun, 2026

Given three sides of a triangle ab and c in the form of integers. Find the area of the triangle. The triangle may not exist. In such cases, the area is 0.
Note: Output the answer upto 3 decimal points.

Examples : 

Input: a = 2, b = 2, c = 3
Output: 1.984
Explanation: The area formed by the triangle whose sides are 2,2 and 3 units respectively is 1.984313 units2.

Input: a = 1, b = 3, c = 1
Output: 0.000
Explanation: Such a triangle does not exist whose sides are 1,3 and 1 respectively. Hence, Area is 0.

Try It Yourself
redirect icon

[Expected Approach] Using Heron's Formula with Validity Check - O(1) Time and O(1) Space

To form a triangle, the sum of any two sides must be strictly greater than the third side. If this condition fails, the triangle does not exist and the area is 0. Otherwise we apply Heron's formula, which computes the area directly from the three side lengths using the semi-perimeter.

Step By Step Implementation:

  • Check the triangle inequality: a + b > c, b + c > a, and a + c > b. If any fails, return area as 0.
  • Compute the semi-perimeter s = (a + b + c) / 2.
  • Apply Heron's formula: area = sqrt(s * (s-a) * (s-b) * (s-c)).
C++
#include <iostream>
#include <cmath>
using namespace std;

double findArea(int a, int b, int c) {

    // Check if the triangle is valid
    if (a + b <= c || b + c <= a || a + c <= b)
        return 0.0;

    // Heron's formula
    double s = (a + b + c) / 2.0;
    double area = sqrt(s * (s - a) * (s - b) * (s - c));

    return area;
}

int main() {
    int a = 2, b = 2, c = 3;
    printf("%.3f\n", findArea(a, b, c));
    return 0;
}
Java
class GfG {

    static double findArea(int a, int b, int c) {

        // Check if the triangle is valid
        if (a + b <= c || b + c <= a || a + c <= b)
            return 0.0;

        // Heron's formula
        double s = (a + b + c) / 2.0;
        double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

        return area;
    }

    public static void main(String[] args) {
        int a = 2, b = 2, c = 3;
        System.out.printf("%.3f%n", findArea(a, b, c));
    }
}
Python
import math

def findArea(a, b, c):

    # Check if the triangle is valid
    if a + b <= c or b + c <= a or a + c <= b:
        return 0.0

    # Heron's formula
    s = (a + b + c) / 2.0
    area = math.sqrt(s * (s - a) * (s - b) * (s - c))

    return area

if __name__ == "__main__":
    a, b, c = 2, 2, 3
    print(f"{findArea(a, b, c):.3f}")
C#
using System;

class GfG {

    static double findArea(int a, int b, int c) {

        // Check if the triangle is valid
        if (a + b <= c || b + c <= a || a + c <= b)
            return 0.0;

        // Heron's formula
        double s = (a + b + c) / 2.0;
        double area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));

        return area;
    }

    static void Main() {
        int a = 2, b = 2, c = 3;
        Console.WriteLine(findArea(a, b, c).ToString("F3"));
    }
}
JavaScript
function findArea(a, b, c) {

    // Check if the triangle is valid
    if (a + b <= c || b + c <= a || a + c <= b)
        return 0.0;

    // Heron's formula
    const s = (a + b + c) / 2.0;
    const area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

    return area;
}

// Driver code
const a = 2, b = 2, c = 3;
console.log(findArea(a, b, c).toFixed(3));

Output
1.984
Comment