C++ Program to Calculate Area of a Right Angled Triangle

Howdy readers, today you will learn how to write a program to calculate the area of a right angled triangle using C++ Programming language.

C++ Program to Calculate Area of a Right Angled Triangle

The area of a right angled triangle is defined as the total space covered by a right-angled triangle. 

The area of a right angled triangle of base ‘b’ and height ‘h’ is:

  • Area of Right Angled Triangle = ½ x Base x Height

This program uses the above formula to calculate the area of a right angled triangle.

So, without any delay, let’s begin this tutorial.

C++ Program to Calculate Area of a Right Angled Triangle

C++ Program

// C++ Program to Calculate Area of a Right Angled Triangle
#include <iostream>
using namespace std;

int main(){
    float base, height, area;
    
    // Asking for input
    cout << "Enter the height of the triangle: ";
    cin >> height;
    cout << "Enter the base of the triangle: ";
    cin >> base;
    
    // Calculating area of the triangle
    area = 0.5 * base * height;
    
    // Displaying result
    cout << "Area of the right angled triangle is: " << area << endl;
    
    return 0;
}

Output

Enter the height of the triangle: 8
Enter the base of the triangle: 6
Area of the right angled triangle is: 24
C++ Program to Calculate Area of a Right Angled Triangle

Explanation

    float base, height, area;

Here in the above program, we have declared three float data type variables named base, height and area.

    // Asking for input
    cout << "Enter the height of the triangle: ";
    cin >> height;
    cout << "Enter the base of the triangle: ";
    cin >> base;

This program displays a message asking the user to enter the height and base of the right angled triangle.

Message is displayed on the screen using cout statement and input is taken with the help of cin statement.

    // Calculating area of the triangle
    area = 0.5 * base * height;

Area is calculated using the formula, area = 0.5 x base x height. The value gets stored in the area  named variable.

    // Displaying result
    cout << "Area of the right angled triangle is: " << area << endl;

The area of the right angled triangle computed above is displayed on the screen using cout statement.

Conclusion

I hope after reading this tutorial, you understand how to write a program to calculate the area of a right angled triangle using C++ Programming language.

If you face any problem while understanding this program, then let us know in the comment section. We will be glad to solve all of your doubts.

Leave a Comment