C hypot()

hypot() function Prototype

double hypot(double p, double b);

h = √(p2+b2) in mathematics is equivalent to h = hypot(p, b); in C Programming.


The hypot() function is defined in math.h header file.


Example: C hypot() Function

#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;
}

Output

hypot(5.00, 12.00) = 13.00

Before we wrap up, let’s put your knowledge of C math hypot() to the test! Can you solve the following challenge?

Challenge:

Write a function to calculate the hypotenuse of a right triangle.

  • Hint: The formula to compute the hypotenuse of the right triangle is {hypotenuse} = sqrt{{side1}^2 + {side2}^2}.
  • For example, with inputs side1 = 3 and side2 = 4, the return value should be 5.00.
Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community