hypot(), hypotf() & hypotl()

We will get Square root of ( x2 + y2 ) of any two numbers by using hypot() function.

Example with integer variable

#include <stdio.h>
#include <math.h>
int main(void){
     int hypt;

     hypt = hypot(3, 4);
     printf("%d", hypt);
     return 0;
}
Ouptut is here
5

Example with float variable using hypotf()

#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

Example with double type variable using hypotl()

#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

Example : without using hypot and by using sqrt()

sqrt()

#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

Example 1: Calculating Hypotenuse Using User Input

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
---

Example 2: Using hypot() for Distance Between Two Points

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
---

Example 3: Comparing Hypotenuse with Direct Calculation

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


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com






    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer