Posted on by Kalkicode
Code Geometric

# Calculate area of right angle triangle

The problem focuses on calculating the area of a right-angle triangle given the lengths of its base and height. This is a fundamental geometric calculation used in various fields such as mathematics, physics, engineering, and architecture. The area of a right-angle triangle can be determined using a simple formula that involves the lengths of the base and height.

## Problem Statement

Given the lengths of the base and height of a right-angle triangle, we need to calculate and output the area of the triangle. The base and height are provided as inputs, and we are required to use the right-angle triangle area formula to find the area of the triangle.

## Example

Consider a right-angle triangle with base = 10.9 and height = 20. This right-angle triangle is represented as follows:

``````
|
|
|\
| \
|  \
10.9   |   \
|    \
|_____\
20``````

## Idea to Solve

To solve this problem, we can follow these steps:

1. Calculate the area of the right-angle triangle using the formula: area = 0.5 * base * height.
2. Use the provided values for the base and height to compute the area.

## Pseudocode

``````function right_angle_triangle_area(base, height):
area = 0.5 * base * height
return area

for each test case:
base, height = right-angle triangle parameters
result = right_angle_triangle_area(base, height)
print("Triangle Size [ base :", base, ", height :", height, "]")
print("Area :", result)``````

## Algorithm Explanation

1. For each test case, calculate the area of the right-angle triangle using the provided formula.
2. Print the triangle's dimensions (base and height) and the calculated area.

## Code Solution

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

///Calculate area of right angle triangle by given base and height
void right_angle_triangle(double base, double height)
{
//Calculate area of right angle triangle
double area = 0.5 * base * height;
//Display the result of right angle triangle area
printf(" Triangle Size [ base : %lf, height : %lf ] ", base, height);
printf("\n Area : %lf\n\n", area);
}
int main()
{
//Test Cases
right_angle_triangle(10.9, 20);
right_angle_triangle(7, 10);
right_angle_triangle(20, 20.6);
right_angle_triangle(4, 8);
return 0;
}``````

#### Output

`````` Triangle Size [ base : 10.900000, height : 20.000000 ]
Area : 109.000000

Triangle Size [ base : 7.000000, height : 10.000000 ]
Area : 35.000000

Triangle Size [ base : 20.000000, height : 20.600000 ]
Area : 206.000000

Triangle Size [ base : 4.000000, height : 8.000000 ]
Area : 16.000000
``````
``````// Java Program
// Calculate area of right angle triangle
class TriangleArea
{
///Calculate area of right angle triangle by given base and height
public void right_angle_triangle(double base, double height)
{
//Calculate area of right angle triangle
double area = 0.5 * base * height;
//Display the result of right angle triangle area
System.out.print(" Triangle Size [ base : " + base + ", height : " + height + " ] ");
System.out.print("\n Area : " + area + "\n\n");
}
public static void main(String[] args)
{
TriangleArea obj = new TriangleArea();
//Test Cases
obj.right_angle_triangle(10.9, 20);
obj.right_angle_triangle(7, 10);
obj.right_angle_triangle(20, 20.6);
obj.right_angle_triangle(4, 8);
}
}``````

#### Output

`````` Triangle Size [ base : 10.9, height : 20.0 ]
Area : 109.0

Triangle Size [ base : 7.0, height : 10.0 ]
Area : 35.0

Triangle Size [ base : 20.0, height : 20.6 ]
Area : 206.0

Triangle Size [ base : 4.0, height : 8.0 ]
Area : 16.0
``````
``````// C++ Program
// Calculate area of right angle triangle
#include<iostream>

using namespace std;
class TriangleArea
{
public:
///Calculate area of right angle triangle by given base and height
void right_angle_triangle(double base, double height)
{
//Calculate area of right angle triangle
double area =( 0.5 * (base * height));
cout << " Triangle Size [ base : " << base << ", height : " << height << " ] ";
cout << "\n Area : " << area << "\n\n";
}
};
int main()
{
TriangleArea obj =  TriangleArea();
//Test Cases
obj.right_angle_triangle(10.9, 20);
obj.right_angle_triangle(7, 10);
obj.right_angle_triangle(20, 20.6);
obj.right_angle_triangle(4, 8);
return 0;
}``````

#### Output

`````` Triangle Size [ base : 10.9, height : 20 ]
Area : 109

Triangle Size [ base : 7, height : 10 ]
Area : 35

Triangle Size [ base : 20, height : 20.6 ]
Area : 206

Triangle Size [ base : 4, height : 8 ]
Area : 16
``````
``````// C# Program
// Calculate area of right angle triangle
using System;
class TriangleArea
{
///Calculate area of right angle triangle by given base and height
public void right_angle_triangle(double triangle_base, double height)
{
//Calculate area of right angle triangle
double area = 0.5 * triangle_base * height;
Console.Write(" Triangle Size [ base : " + triangle_base + ", height : " + height + " ] ");
Console.Write("\n Area : " + area + "\n\n");
}
public static void Main(String[] args)
{
TriangleArea obj = new TriangleArea();
//Test Cases
obj.right_angle_triangle(10.9, 20);
obj.right_angle_triangle(7, 10);
obj.right_angle_triangle(20, 20.6);
obj.right_angle_triangle(4, 8);
}
}``````

#### Output

`````` Triangle Size [ base : 10.9, height : 20 ]
Area : 109

Triangle Size [ base : 7, height : 10 ]
Area : 35

Triangle Size [ base : 20, height : 20.6 ]
Area : 206

Triangle Size [ base : 4, height : 8 ]
Area : 16
``````
``````<?php
// Php Program
// Calculate area of right angle triangle
class TriangleArea
{
///Calculate area of right angle triangle by given base and height
public  function right_angle_triangle(\$base, \$height)
{
//Calculate area of right angle triangle
\$area = 0.5 *\$base *\$height;
//Display the result of right angle triangle area
echo(" Triangle Size [ base : ". \$base .", height : ". \$height ." ] ");
echo("\n Area : ". \$area ."\n\n");
}
}

function main()
{
\$obj = new TriangleArea();
//Test Cases
\$obj->right_angle_triangle(10.9, 20);
\$obj->right_angle_triangle(7, 10);
\$obj->right_angle_triangle(20, 20.6);
\$obj->right_angle_triangle(4, 8);
}
main();``````

#### Output

`````` Triangle Size [ base : 10.9, height : 20 ]
Area : 109

Triangle Size [ base : 7, height : 10 ]
Area : 35

Triangle Size [ base : 20, height : 20.6 ]
Area : 206

Triangle Size [ base : 4, height : 8 ]
Area : 16
``````
``````// Node Js Program
// Calculate area of right angle triangle
class TriangleArea
{
///Calculate area of right angle triangle by given base and height
right_angle_triangle(base, height)
{
//Calculate area of right angle triangle
var area = 0.5 *base *height;
//Display the result of right angle triangle area
process.stdout.write(" Triangle Size [ base : " + base + ", height : " + height + " ] ");
process.stdout.write("\n Area : " + area + "\n\n");
}
}

function main(args)
{
var obj = new TriangleArea();
//Test Cases
obj.right_angle_triangle(10.9, 20);
obj.right_angle_triangle(7, 10);
obj.right_angle_triangle(20, 20.6);
obj.right_angle_triangle(4, 8);
}
main();``````

#### Output

`````` Triangle Size [ base : 10.9, height : 20 ]
Area : 109

Triangle Size [ base : 7, height : 10 ]
Area : 35

Triangle Size [ base : 20, height : 20.6 ]
Area : 206

Triangle Size [ base : 4, height : 8 ]
Area : 16
``````
``````#  Python 3 Program
#  Calculate area of right angle triangle
class TriangleArea :
# /Calculate area of right angle triangle by given base and height
def right_angle_triangle(self, base, height) :
# Calculate area of right angle triangle
area = 0.5 * base * height
# Display the result of right angle triangle area
print(" Triangle Size [ base : ", base ,", height : ", height ," ] ", end = "")
print("\n Area : ", area ,"\n\n", end = "")

def main() :
obj = TriangleArea()
# Test Cases
obj.right_angle_triangle(10.9, 20)
obj.right_angle_triangle(7, 10)
obj.right_angle_triangle(20, 20.6)
obj.right_angle_triangle(4, 8)

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

#### Output

`````` Triangle Size [ base :  10.9 , height :  20  ]
Area :  109.0

Triangle Size [ base :  7 , height :  10  ]
Area :  35.0

Triangle Size [ base :  20 , height :  20.6  ]
Area :  206.0

Triangle Size [ base :  4 , height :  8  ]
Area :  16.0
``````
``````#  Ruby Program
#  Calculate area of right angle triangle
class TriangleArea

# /Calculate area of right angle triangle by given base and height
def right_angle_triangle(base, height)

# Calculate area of right angle triangle
area = 0.5 * base * height
# Display the result of right angle triangle area
print(" Triangle Size [ base  :", base ,", height  :", height ," ] ")
print("\n Area  :", area ,"\n\n")
end
end
def main()

obj = TriangleArea.new()
# Test Cases
obj.right_angle_triangle(10.9, 20)
obj.right_angle_triangle(7, 10)
obj.right_angle_triangle(20, 20.6)
obj.right_angle_triangle(4, 8)
end
main()``````

#### Output

`````` Triangle Size [ base  :10.9, height  :20 ]
Area  :109.0

Triangle Size [ base  :7, height  :10 ]
Area  :35.0

Triangle Size [ base  :20, height  :20.6 ]
Area  :206.0

Triangle Size [ base  :4, height  :8 ]
Area  :16.0

``````
``````// Scala Program
// Calculate area of right angle triangle
class TriangleArea
{
///Calculate area of right angle triangle by given base and height
def right_angle_triangle(base: Double, height: Double): Unit = {
//Calculate area of right angle triangle
var area: Double = 0.5 * base * height;
//Display the result of right angle triangle area
print(" Triangle Size [ base : " + base + ", height : " + height + " ] ");
print("\n Area : " + area + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: TriangleArea = new TriangleArea();
//Test Cases
obj.right_angle_triangle(10.9, 20);
obj.right_angle_triangle(7, 10);
obj.right_angle_triangle(20, 20.6);
obj.right_angle_triangle(4, 8);
}
}``````

#### Output

`````` Triangle Size [ base : 10.9, height : 20.0 ]
Area : 109.0

Triangle Size [ base : 7.0, height : 10.0 ]
Area : 35.0

Triangle Size [ base : 20.0, height : 20.6 ]
Area : 206.0

Triangle Size [ base : 4.0, height : 8.0 ]
Area : 16.0
``````
``````// Swift Program
// Calculate area of right angle triangle
class TriangleArea
{
///Calculate area of right angle triangle by given base and height
func right_angle_triangle(_ base: Double, _ height: Double)
{
//Calculate area of right angle triangle
let area: Double = 0.5 * base * height;
//Display the result of right angle triangle area
print(" Triangle Size [ base : ", base ,", height : ", height ," ] ", terminator: "");
print("\n Area : ", area ,"\n\n", terminator: "");
}
}
func main()
{
let obj: TriangleArea = TriangleArea();
//Test Cases
obj.right_angle_triangle(10.9, 20);
obj.right_angle_triangle(7, 10);
obj.right_angle_triangle(20, 20.6);
obj.right_angle_triangle(4, 8);
}
main();``````

#### Output

`````` Triangle Size [ base :  10.9 , height :  20.0  ]
Area :  109.0

Triangle Size [ base :  7.0 , height :  10.0  ]
Area :  35.0

Triangle Size [ base :  20.0 , height :  20.6  ]
Area :  206.0

Triangle Size [ base :  4.0 , height :  8.0  ]
Area :  16.0
``````

## Output Explanation

The provided code runs four test cases and calculates the areas of right-angle triangles with different base lengths and heights. The output includes the triangle's dimensions and the corresponding calculated areas.

## Time Complexity

The time complexity of this code is O(1) for each test case. The calculations involve basic arithmetic operations, 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