sqrt() in C: Square Root

sqrt() returns the non-negative square root of a floating-point value. For ordinary real-number calculations, the argument must be zero or positive.

Syntax Top ↑

double sqrt(double x);

Square Root Example Top ↑

#include <math.h>
#include <stdio.h>

int main(void) {
    double value = 144.0;

    printf("Square root: %.2f\n", sqrt(value));
    return 0;
}

Output

Square root: 12.00

Read a Value Safely Top ↑

#include <math.h>
#include <stdio.h>

int main(void) {
    double value;

    printf("Enter a non-negative number: ");
    if (scanf("%lf", &value) != 1) {
        printf("Invalid input.\n");
        return 1;
    }

    if (value < 0.0) {
        printf("No real square root for a negative value.\n");
        return 1;
    }

    printf("sqrt = %.6f\n", sqrt(value));
    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