C Program - Square Root of a Number

If a number is multiplied by itself (n*n), the final number will be the square of that number and finding the square root of a number is inverse operation of squaring the number. If x is the square root of y, it can be expressed as below: Alternatively, it can also be expressed as:

x2 = y

Method 1: Using sqrt() function of C <math.h> header file

The sqrt() function of C <math.h> header file can be used to return square root of a number.

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

int main() {
double x = 16;
double y = 25;

double x1 = sqrt(x);
double y1 = sqrt(y);
printf("Square root of %.2f is %.2f\n", x, x1);
printf("Square root of %.2f is %.2f\n", y, y1);
}

The above code will give the following output:

Square root of 16.00 is 4.00
Square root of 25.00 is 5.00

Method 2: Using pow() function of C <math.h> header file

The pow() function of C <math.h> header file can also be used to calculate square root of a number.

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

int main() {
double x = 16;
double y = 25;

double x1 = pow(x, 0.5);
double y1 = pow(y, 0.5);
printf("Square root of %.2f is %.2f\n", x, x1);
printf("Square root of %.2f is %.2f\n", y, y1);
}

The above code will give the following output:

Square root of 16.00 is 4.00
Square root of 25.00 is 5.00

5