hypot()
function.
#include <stdio.h>
#include <math.h>
int main(void){
int hypt;
hypt = hypot(3, 4);
printf("%d", hypt);
return 0;
}
Ouptut is here
5
#include <stdio.h>
#include <math.h>
int main(void){
float hypt;
hypt = hypotf(5, 4);
printf("%f", hypt);
return 0;
}
The above code will give this output.
6.403124
#include <stdio.h>
#include <math.h>
int main(void){
double hypt;
hypt = hypotl(7, 9);
printf("%f", hypt);
return 0;
}
Output is here
11.401754
#include <stdio.h>
#include <math.h>
int main(void){
double hypt;
hypt = sqrt((7*7)+ (9*9));
printf("%f", hypt);
return 0;
}
Output
11.401754
This example demonstrates how to calculate the hypotenuse by taking user input for the two sides of a right-angled triangle.
#include <stdio.h>
#include <math.h>
int main() {
double side1, side2, hypotenuse;
printf("Enter the lengths of two sides: ");
scanf("%lf %lf", &side1, &side2);
hypotenuse = hypot(side1, side2);
printf("The hypotenuse is: %.2f\n", hypotenuse);
return 0;
}
Output:
Enter the lengths of two sides: 3 4
The hypotenuse is: 5.00
---
The hypot() function can also be used to calculate the distance between two points on a 2D plane.
#include <stdio.h>
#include <math.h>
int main() {
double x1 = 3, y1 = 4, x2 = 7, y2 = 1;
double distance;
distance = hypot(x2 - x1, y2 - y1);
printf("Distance between the points: %.2f\n", distance);
return 0;
}
Output:
Distance between the points: 5.00
---
This example compares the result of hypot() with manually calculating the hypotenuse using the Pythagorean theorem.
#include <stdio.h>
#include <math.h>
int main() {
double side1 = 5, side2 = 12, hypot_result, manual_result;
hypot_result = hypot(side1, side2);
manual_result = sqrt((side1 * side1) + (side2 * side2));
printf("Using hypot(): %.2f\n", hypot_result);
printf("Manual calculation: %.2f\n", manual_result);
return 0;
}
Output:
Using hypot(): 13.00
Manual calculation: 13.00