Find the dimensions of Right angled triangle in C++

C++Server Side ProgrammingProgramming

In this problem, we are given two values H and A, denoting the hypotenuse and area of a right angled triangle. Our task is to find the dimensions of the Right angled triangle.

Right Angled Triangle is a special type of triangle in which two sides intersect at right angles.

Fig : Right Angle Triangle

Let's take an example to understand the problem,

Input : H = 7 , A = 8
Output : height = 2.43, base = 6.56

Solution Approach

Solution to this problem can be found by using the mathematical formula for the values. And lets derive them here,



Using the formula,




Similarly using the formula,




Here, we have two equations,

On adding both we have




On subtracting both we get,




Applying both formulas to get the values of b and h.


Program to illustrate the working of our solution

#include <iostream>
#include <math.h>
using namespace std;
void findAllDismensionsRightTriangle(int H, int A) {
   if (H * H < 4 * A) {
      cout<<"Not Possible\n";
   float val1 = (float)sqrt(H * H + 4 * A);
   float val2 = (float)sqrt(H * H - 4 * A);
   float b = (float)(val1 + val2) / 2.0;
   float p = (float)(val1 - val2) / 2.0;
   cout<<"Perpendicular = "<<p<<endl; 
   cout<<"Base = "<<b;
int main() {
   int H = 7;
   int A = 8;
   cout<<"The dimensions of the triangle are : \n"; 
   cout<<"Hypotenuse = "<<H<<endl;
   findAllDismensionsRightTriangle(H, A);
   return 0;


The dimensions of the triangle are :
Hypotenuse = 7
Perpendicular = 2.43845
Base = 6.56155
Updated on 27-Jan-2022 10:37:20