Posted on by Kalkicode
Code Geometric

# Calculate area of triangle

The problem at hand involves calculating the area of a triangle given the lengths of its three sides. This is a fundamental geometric calculation that is often required in various applications, ranging from architecture and engineering to computer graphics and physics. The area of a triangle can be determined using the well-known formula involving Heron's formula, which is based on the semiperimeter of the triangle.

## Problem Statement

Given the lengths of three sides of a triangle (a, b, and c), we need to calculate and output the area of the triangle. The sides of the triangle are provided as inputs, and we are required to use Heron's formula to find the area of the triangle.

## Example

Let's consider an example where we have a triangle with sides a = 24.2, b = 13.1, and c = 24. This triangle's sides correspond to the following configuration:

``````
24.2        .
③      .   . ②
.        .   13.1
.              .
-------------------
①
24
``````

## Idea to Solve

To solve this problem, we can use the following steps:

1. Calculate the semiperimeter (s) of the triangle using the formula: s = (a + b + c) / 2.
2. Using Heron's formula, calculate the area (A) of the triangle: A = sqrt(s * (s - a) * (s - b) * (s - c)), where `sqrt` represents the square root function.

## Pseudocode

``````function triangle_area(a, b, c):
s = (a + b + c) / 2
area = sqrt(s * (s - a) * (s - b) * (s - c))
return area

for each test case:
a, b, c = triangle sides
result = triangle_area(a, b, c)
print("Triangle Sides [ a :", a, ", b :", b, "c :", c, "]")
print("Triangle Area :", result)``````

## Algorithm Explanation

1. For each test case, calculate the semiperimeter (s) using the formula (a + b + c) / 2.
2. Use Heron's formula to calculate the area (A) using the semiperimeter and side lengths.
3. Print the side lengths and the calculated area.

## Code Solution

``````// C Program
// Calculate area of triangle
#include <stdio.h>
#include <math.h>

//Calculate area of triangle by given three side
void triangle_area(double a,double b,double c)
{

double side = (a + b + c)/2;

//Calculate area of triangle
double area = sqrt(side *((side-a)*(side-b)*(side-c)));

//Display the result of triangle area
printf(" Triangle Sides [ a : %lf, b : %lf c : %lf ] ",a,b,c );
printf("\n Triangle Area : %lf\n\n", area);
}

int main()
{
/*

24.2        .
③      .   . ②
.        .   13.1
.              .
-------------------
①
24
*/
//Test Cases
triangle_area(24,13.1,24.2);

triangle_area(22.2,20.2,7.7);

triangle_area(12.8,13.7,15.9);

return 0;
}``````

#### Output

`````` Triangle Sides [ a : 24.000000, b : 13.100000 c : 24.200000 ]
Triangle Area : 151.895350

Triangle Sides [ a : 22.200000, b : 20.200000 c : 7.700000 ]
Triangle Area : 77.508067

Triangle Sides [ a : 12.800000, b : 13.700000 c : 15.900000 ]
Triangle Area : 84.134892
``````
``````/*
Java program
Calculate area of triangle
*/
public class TriangleArea
{
//Calculate area of triangle by given three side
public void triangle_area(double a, double b, double c)
{
double side = (a + b + c) / 2;
//Calculate area of triangle
double area = Math.sqrt(side * ((side - a) * (side - b) * (side - c)));
//Display the result of triangle area
System.out.print(" Triangle Sides [ a : " + a + ", b : " + b + " c : " + c + " ] ");
System.out.print("\n Triangle Area : " + area + "\n\n");
}
public static void main(String[] args)
{
TriangleArea obj = new TriangleArea();
/*

24.2        .
③      .   . ②
.        .   13.1
.              .
-------------------
①
24
*/
//Test Cases
obj.triangle_area(24, 13.1, 24.2);
obj.triangle_area(22.2, 20.2, 7.7);
obj.triangle_area(12.8, 13.7, 15.9);
}
}``````

#### Output

`````` Triangle Sides [ a : 24.0, b : 13.1 c : 24.2 ]
Triangle Area : 151.89535030325973

Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
Triangle Area : 77.50806663664115

Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
Triangle Area : 84.13489169185397
``````
``````/*
C++ program
Calculate area of triangle
*/
#include<iostream>
#include <math.h>
using namespace std;
class TriangleArea
{
public:
//Calculate area of triangle by given three side
void triangle_area(double a, double b, double c)
{
double side = (a + b + c) / 2;
//Calculate area of triangle
double area = sqrt(side *((side - a) *(side - b) *(side - c)));
cout << " Triangle Sides [ a : " << a << ", b : " << b << " c : " << c << " ] ";
cout << "\n Triangle Area : " << area << "\n\n";
}
};
int main()
{
TriangleArea obj = TriangleArea();
/*

24.2        .
③      .   . ②
.        .   13.1
.              .
-------------------
①
24
*/
//Test Cases
obj.triangle_area(24, 13.1, 24.2);
obj.triangle_area(22.2, 20.2, 7.7);
obj.triangle_area(12.8, 13.7, 15.9);
return 0;
}``````

#### Output

`````` Triangle Sides [ a : 24, b : 13.1 c : 24.2 ]
Triangle Area : 151.895

Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
Triangle Area : 77.5081

Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
Triangle Area : 84.1349
``````
``````/*
C# program
Calculate area of triangle
*/
using System;
public class TriangleArea
{
//Calculate area of triangle by given three side
public void triangle_area(double a, double b, double c)
{
double side = (a + b + c) / 2;
//Calculate area of triangle
double area = Math.Sqrt(side * ((side - a) * (side - b) * (side - c)));
Console.Write(" Triangle Sides [ a : " + a + ", b : " + b + " c : " + c + " ] ");
Console.Write("\n Triangle Area : " + area + "\n\n");
}
public static void Main(String[] args)
{
TriangleArea obj = new TriangleArea();
/*

24.2        .
③      .   . ②
.        .   13.1
.              .
-------------------
①
24
*/
//Test Cases
obj.triangle_area(24, 13.1, 24.2);
obj.triangle_area(22.2, 20.2, 7.7);
obj.triangle_area(12.8, 13.7, 15.9);
}
}``````

#### Output

`````` Triangle Sides [ a : 24, b : 13.1 c : 24.2 ]
Triangle Area : 151.89535030326

Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
Triangle Area : 77.5080666366411

Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
Triangle Area : 84.134891691854
``````
``````<?php
/*
Php program
Calculate area of triangle
*/
class TriangleArea
{
//Calculate area of triangle by given three side
public 	function triangle_area(\$a, \$b, \$c)
{
\$side = (\$a + \$b + \$c) / 2;
//Calculate area of triangle
\$area = sqrt(\$side *((\$side - \$a) *(\$side - \$b) *(\$side - \$c)));
//Display the result of triangle area
echo(" Triangle Sides [ a : ". \$a .", b : ". \$b ." c : ". \$c ." ] ");
echo("\n Triangle Area : ". \$area ."\n\n");
}
}

function main()
{
\$obj = new TriangleArea();
/*

24.2        .
③      .   . ②
.        .   13.1
.              .
-------------------
①
24
*/
//Test Cases
\$obj->triangle_area(24, 13.1, 24.2);
\$obj->triangle_area(22.2, 20.2, 7.7);
\$obj->triangle_area(12.8, 13.7, 15.9);
}
main();``````

#### Output

`````` Triangle Sides [ a : 24, b : 13.1 c : 24.2 ]
Triangle Area : 151.89535030326

Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
Triangle Area : 77.508066636641

Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
Triangle Area : 84.134891691854
``````
``````/*
Node Js program
Calculate area of triangle
*/
class TriangleArea
{
//Calculate area of triangle by given three side
triangle_area(a, b, c)
{
var side = (a + b + c) / 2;
//Calculate area of triangle
var area = Math.sqrt(side *((side - a) *(side - b) *(side - c)));
//Display the result of triangle area
process.stdout.write(" Triangle Sides [ a : " + a + ", b : " + b + " c : " + c + " ] ");
process.stdout.write("\n Triangle Area : " + area + "\n\n");
}
}

function main(args)
{
var obj = new TriangleArea();
/*

24.2        .
③      .   . ②
.        .   13.1
.              .
-------------------
①
24
*/
//Test Cases
obj.triangle_area(24, 13.1, 24.2);
obj.triangle_area(22.2, 20.2, 7.7);
obj.triangle_area(12.8, 13.7, 15.9);
}
main();``````

#### Output

`````` Triangle Sides [ a : 24, b : 13.1 c : 24.2 ]
Triangle Area : 151.89535030325973

Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
Triangle Area : 77.50806663664115

Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
Triangle Area : 84.13489169185397
``````
``````#   Python 3 program
#   Calculate area of triangle
import math
class TriangleArea :
# Calculate area of triangle by given three side
def triangle_area(self, a, b, c) :
side = (a + b + c) / 2
# Calculate area of triangle
area = math.sqrt(side * ((side - a) * (side - b) * (side - c)))

# Display the result of triangle area
print(" Triangle Sides [ a : ", a ,", b : ", b ," c : ", c ," ] ", end = "")
print("\n Triangle Area : ", area ,"\n\n", end = "")

def main() :
obj = TriangleArea()
# Test Cases
#
# 		  24.2        .
# 		    ③      .   . ②
# 		        .        .   13.1
# 		    .              .
# 		  -------------------
# 		        ①
# 		           24
#

obj.triangle_area(24, 13.1, 24.2)
obj.triangle_area(22.2, 20.2, 7.7)
obj.triangle_area(12.8, 13.7, 15.9)

if __name__ == "__main__": main()``````

#### Output

`````` Triangle Sides [ a :  24 , b :  13.1  c :  24.2  ]
Triangle Area :  151.89535030325973

Triangle Sides [ a :  22.2 , b :  20.2  c :  7.7  ]
Triangle Area :  77.50806663664115

Triangle Sides [ a :  12.8 , b :  13.7  c :  15.9  ]
Triangle Area :  84.13489169185397
``````
``````#   Ruby program
#   Calculate area of triangle

class TriangleArea

# Calculate area of triangle by given three side
def triangle_area(a, b, c)

side = (a + b + c) / 2
# Calculate area of triangle
area = Math.sqrt(side * ((side - a) * (side - b) * (side - c)))
# Display the result of triangle area
print(" Triangle Sides [ a  : ", a ,", b  : ", b ," c  : ", c ," ] ")
print("\n Triangle Area  : ", area ,"\n\n")
end
end
def main()

obj = TriangleArea.new()
# Test Cases
#
# 		  24.2        .
# 		    ③      .   . ②
# 		        .        .   13.1
# 		    .              .
# 		  -------------------
# 		        ①
# 		           24
#

obj.triangle_area(24, 13.1, 24.2)
obj.triangle_area(22.2, 20.2, 7.7)
obj.triangle_area(12.8, 13.7, 15.9)
end
main()``````

#### Output

`````` Triangle Sides [ a  : 24, b  : 13.1 c  : 24.2 ]
Triangle Area  : 151.89535030325973

Triangle Sides [ a  : 22.2, b  : 20.2 c  : 7.7 ]
Triangle Area  : 77.50806663664115

Triangle Sides [ a  : 12.8, b  : 13.7 c  : 15.9 ]
Triangle Area  : 84.13489169185397

``````
``````/*
Scala program
Calculate area of triangle
*/
class TriangleArea
{
//Calculate area of triangle by given three side
def triangle_area(a: Double, b: Double, c: Double): Unit = {
var side: Double = (a + b + c) / 2;
//Calculate area of triangle
var area: Double = Math.sqrt(side * ((side - a) * (side - b) * (side - c)));
//Display the result of triangle area
print(" Triangle Sides [ a : " + a + ", b : " + b + " c : " + c + " ] ");
print("\n Triangle Area : " + area + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: TriangleArea = new TriangleArea();
/*

24.2        .
③      .   . ②
.        .   13.1
.              .
-------------------
①
24
*/
//Test Cases
obj.triangle_area(24, 13.1, 24.2);
obj.triangle_area(22.2, 20.2, 7.7);
obj.triangle_area(12.8, 13.7, 15.9);
}
}``````

#### Output

`````` Triangle Sides [ a : 24.0, b : 13.1 c : 24.2 ]
Triangle Area : 151.89535030325973

Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
Triangle Area : 77.50806663664115

Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
Triangle Area : 84.13489169185397
``````
``````/*
Swift program
Calculate area of triangle
*/
import Foundation
class TriangleArea
{
//Calculate area of triangle by given three side
func triangle_area(_ a: Double, _ b: Double, _ c: Double)
{
let side: Double = (a + b + c) / 2;
//Calculate area of triangle
let area: Double = sqrt(side * ((side - a) * (side - b) * (side - c)));

//Display the result of triangle area
print(" Triangle Sides [ a : ", a ,", b : ", b ," c : ", c ," ] ", terminator: "");
print("\n Triangle Area : ", area ,"\n\n", terminator: "");
}
}
func main()
{
let obj: TriangleArea = TriangleArea();
/*

24.2        .
③      .   . ②
.        .   13.1
.              .
-------------------
①
24
*/
//Test Cases
obj.triangle_area(24, 13.1, 24.2);
obj.triangle_area(22.2, 20.2, 7.7);
obj.triangle_area(12.8, 13.7, 15.9);
}
main();``````

#### Output

`````` Triangle Sides [ a :  24.0 , b :  13.1  c :  24.2  ]
Triangle Area :  151.89535030326

Triangle Sides [ a :  22.2 , b :  20.2  c :  7.7  ]
Triangle Area :  77.5080666366411

Triangle Sides [ a :  12.8 , b :  13.7  c :  15.9  ]
Triangle Area :  84.134891691854
``````

## Output Explanation

The provided code runs three test cases and calculates the areas of the triangles with different side lengths. The output includes the side lengths of the triangles along with their corresponding calculated areas.

## Time Complexity

The time complexity of this code is O(1) for each test case. The calculations involve basic arithmetic operations and a single square root computation, all of which take constant time. Therefore, the overall time complexity remains constant for each test case.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post