C Program For Hypotenuse Of Right Angled Triangle

Hi, Welcome to our new post which is C Program for hypotenuse of Right Angled Triangle.

We are going to discuss the algorithm and then we code it in C language. You can use any language you want after understanding.

Before discussing program let’s understand what is Hypotenuse in right angled triangle. You also read the program for Pythagoras theorem.

Below is the figure of Right angled triangle where ABC is 90 degree.

Right Angled triangle always satisfy

Hypotenuse * Hypotenuse = Perpendicular * Perpendicular + Base * Base

From the above figure

a * a = b* b + c* c

Now, you know what is the hypotenuse, perpendicular and Base It is very easy to write algorithm and Program

We discuss the program for Hypotenuse in 3 ways –

Program for Hypotenuse Using User Defined Function

#include <stdio.h>

double hypotenuse(double p, double b)
{
    return p * p + b * b;
}

int main() {

double perp = 3;
double base = 4;


double hypsquare = hypotenuse(perp,base);
printf("Hypotenuse is %f",sqrt(hypsquare));
	return 0;
}

Program Explanation

  1. Explanation is very simple, we define a custom function name as hypotenuse which accept base and perpendicular as argument.
  2. We pass perp and base and receive hypotenuse and we print in the printf Function with help of %f format specifier because return is double.
  3. As what we recieved from the hypotenuse function is square of the hypotenuse, we need to find the square root of value.
  4. For finding square root we use Sqrt function defined in math.h

Program for Hypotenuse using pow Function

#include <stdio.h>
#include <math.h>
int main() {
double perp = 3;
double base = 4;
double hypsquare = pow(perp,perp) + pow(base,base);
printf("Hypotenuse is %f",sqrt(hypsquare));
	return 0;
}

Program Explanation

  1. We use pow function to find square which is define in math.h header file.
  2. Pow function takes two argument and raise the second argument as power of first argument
  3. Pow(a,b) means a^b. Example pow(3,2) means 3^2 = 9
  4. We do same for perp and base and then add to find Hypotenuse square
  5. After getting hypotenuse square we use sqrt function to find square root of hypotenuse
  6. Sqrt function also define in math.h and it accepts only one argument and return square root of number.

Program for Hypotenuse using hypot Function

If you are having problem with finding square and then adding and the finding square root, then there is a better option provided by math.h library

Hypot Function

This function is used to find the hypotenuse. This function takes perpendicular and base as an argument and provide your the hypotenuse directly. You don’t need to do square and square root, this function do it for you.

#include <stdio.h>
#include <math.h>

int main()
{
    double p, b;
    double hypotenuse;

    p = 5.0;
    b = 12.0;

    hypotenuse = hypot(p, b);

    printf("hypot(%.2lf, %.2lf) = %.2lf", p, b, hypotenuse);

    return 0;
}

Program is very simple and I think you don’t need any explanation.

Note: We don’t use scanf Function to take input from user, if you need you can use, You can also use getline function in c

Conclusion

We here discuss all the three way of finding thee hypotenuse, You can use any of them. Hypot function is easy to use but not acceptable in college.

Use sqrt function method in your college exams if asks.

Hope you love C Program For Hypotenuse Of Right Angled Triangle, Feel free to share and comment.

Leave a Comment

Your email address will not be published. Required fields are marked *