hypot(), hypotf() and hypotl() in C

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.

Syntax Top ↑

double hypot(double x, double y);
float hypotf(float x, float y);
long double hypotl(long double x, long double y);

3-4-5 Triangle Top ↑

#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

Distance from the Origin Top ↑

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

Preserved hypot() Variations Top ↑

hypotf() and hypotl()

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

Compare hypot() with sqrt(x*x + y*y)

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

Distance Between Two Points

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

User-Entered Sides

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




plus2net.com






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