C++ Program to Calculate Area of Right Angle Triangle


Introduction:

In geometry, a triangle is a three-sided polygon with three angles. Right angle triangle is a special type of triangle which has one angle of 90 degrees. The area of a right angle triangle is half of the product of its base and height. In this article, we will discuss how to calculate the area of a right angle triangle using C++ programming language

Problem Statement:

Write a C++ program to calculate the area of a right angle triangle, given its base and height.

Solution:

To calculate the area of a right angle triangle in C++, we need to take two inputs from the user: base and height. After that, we will use the formula for the area of a right angle triangle, which is half of the product of its base and height.

Let's see the code:

#include <iostream>
using namespace std;

int main()
{
    float base, height, area;

    cout << "Enter base and height of the triangle: ";
    cin >> base >> height;

    area = 0.5 * base * height;

    cout << "Area of the triangle = " << area;

    return 0;
}

Explanation:

In the above code, we have used the `iostream` library to include the `cout` and `cin` objects for input and output operations. We have also used the `using namespace std;` statement to avoid the prefix `std::` before the objects.

Then, we have declared three float variables `base`, `height`, and `area` to store the values of base, height, and area respectively. 

Next, we have prompted the user to enter the base and height of the triangle using `cout` and read them from the keyboard using `cin`. 

Also Read : Calculate Area of Right angle Triangle using C Language

After that, we have used the formula for the area of a right angle triangle, which is half of the product of its base and height, and assigned the result to the `area` variable.

Finally, we have printed the value of the area of the triangle using `cout`.

Testing:
Let's test the program with some sample inputs.

Test Case 1:
Enter base and height of the triangle: 5 4
Area of the triangle = 10

Test Case 2:
Enter base and height of the triangle: 10 12
Area of the triangle = 60

Test Case 3:
Enter base and height of the triangle: 7.5 6.2
Area of the triangle = 23.25

All the test cases are working fine and giving the correct output. 

Conclusion:

In this article, we have discussed how to calculate the area of a right angle triangle using C++ programming language. We have used the formula for the area of a right angle triangle, which is half of the product of its base and height. We have also tested the program with some sample inputs and found that it is working fine.

       

Advertisements

ads