hypot() calculates sqrt(x*x + y*y) for two floating-point values and is commonly used to find the hypotenuse of a right triangle. C also provides hypotf() for float and hypotl() for long double.
double hypot(double x, double y);
float hypotf(float x, float y);
long double hypotl(long double x, long double y);
#include <math.h>
#include <stdio.h>
int main(void) {
double a = 3.0;
double b = 4.0;
printf("Hypotenuse: %.2f\n", hypot(a, b));
return 0;
}
Output
Hypotenuse: 5.00
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 6.0;
double y = 8.0;
printf("Distance: %.2f\n", hypot(x, y));
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
float f = hypotf(5.0f, 4.0f);
long double ld = hypotl(7.0L, 9.0L);
printf("hypotf(5, 4) = %.6f\n", f);
printf("hypotl(7, 9) = %.6Lf\n", ld);
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 7.0;
double y = 9.0;
double using_hypot = hypot(x, y);
double manual = sqrt(x * x + y * y);
printf("Using hypot(): %.6f\n", using_hypot);
printf("Using sqrt(): %.6f\n", manual);
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
double x1 = 3.0;
double y1 = 4.0;
double x2 = 7.0;
double y2 = 1.0;
double distance = hypot(x2 - x1, y2 - y1);
printf("Distance: %.2f\n", distance);
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
double first;
double second;
printf("Enter the lengths of two perpendicular sides: ");
if (scanf("%lf %lf", &first, &second) != 2) {
printf("Invalid input.\n");
return 1;
}
printf("Hypotenuse: %.2f\n", hypot(first, second));
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.