# How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number.

A square root is a mathematical term. The square root of a number is the inverse of the square of the number. A number's square root is a value that yields the original number when multiplied by itself.

It describes returning the square of a number when the same is multiplied by itself.

Example

Let us take the number 4; 4 multiplied by 4 is 16.

4 * 4 = 16.

In the above case, 4 is the square root for the number 16.

Similarly, let us take another number, 2. 2 multiplied by 2 is 4.

2 * 2 = 4.

In the above case, 2 is the square root for the number 4.

## What is sqrt() function?

The sqrt() function is used in the C language to determine the square root of an integer.

The sqrt() function is a built-in library function in the C programming language used to determine a number's square root. The math. h header file defines the sqrt() function. Therefore, to use the sqrt() function in C, we must create the math.h header file. In addition, we don't need to use the sqrt function to determine the supplied number's square root.

Syntax

`double sqrt(double x);`

Here, the argument is x. It is a number used to calculate the square root of x.

The sqrt() method takes a single double datatype input and returns the square root as a double data type using the above syntax.

By explicitly converting the provided data type to another, it is possible to determine the square root of a number of the int, float, double, or long double data types.

## Algorithm for Finding the Square Root

1. Create an integer variable called "n."
2. To determine the square root, use the variable “n” as an argument in the sqrt() function.
3. Print the outcome.
4. Exit or close the program.

Example 1

In this program, the output is the square root of the number 100.

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

int main ()
{
// declaring an integer variable. Here, the variable is named "a."
int a = 100 ;
double result ;
// use the sqrt() function to return integer values
result = sqrt(a) ;
printf (" \n The square root of the number %d is: %.2lf", a, result) ;

return 0 ;
}
``````

Output In the above-displayed screenshot, the output is the square root of the number 100. The value 10 is the square root of the number 100.

Example 2

A program calculates a number's square root using the sqrt() function.

In this program, the user gives the input for int, float, and double datatypes.

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

int main ()
{
// the int, double, and float variables are declared
int a, result ;
float b, result1 ;
double c, result2 ;

printf (" Enter the numbers to find their square root: ") ;
scanf("%d",&a) ;
// sqrt() function is used. Here, it returns an integer value.
result = sqrt(a) ;
printf ("The square root of the number %d is: %d", a, result) ;
scanf("%f",&b) ;
// finding square root for the floating-point values.
// sqrt() function is used. Here, it returns the float value.
result1 = sqrt(b) ;
printf (" \nThe square root of %.2f is: %.2f", b, result1) ;

// finding square root for the floating-point values for the double datatype.
scanf("%lf",&c) ;
// sqrt() function is used. Here, it returns the double value.
result2 = sqrt(c) ;
printf (" \nThe square root of %.2lf is: %.2lf", c, result2) ;

return 0 ;
}

``````

The above program represents the sqrt() function.

Output 1 In the above-displayed screenshot, the user input is 9 (int value), 36.00 (float value), and 64.0000 (double value).

The value 3 is the square root for the value 9.

The value 6 is the square root for the value 36.

The value 8 is the square root for the value 64.

Output 2 In the above-displayed screenshot, the user input is 4 (int value), 2.25 (float value), and 6.2500 (double value).

The value 2 is the square root for the value 4.

The value 1.50 is the square root for the value 2.25.

The value 2.50 is the square root for the value 6.25.

Output 3 In the above-displayed screenshot, the user input is 25 (int value), 3.00 (float value), and 64.6400 (double value).

The value 5 is the square root for the value 25.

The value 1.73 is the square root for the value 3.00.

The value 8.04 is the square root for the value 64.64.

Example 3

Let's write a program in the C programming language that uses the user-defined function to calculate the value's square root.

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

// declaring the function Sqrtfunc().
double Sqrtfunc (int n) ;

int main ()
{
// declaring an integer variable x.
int x ;
double result ;
printf (" Enter any number to get the square root: ") ;
scanf (" %d", &x) ;

result = Sqrtfunc(x) ; // calling the function  Sqrtfunc().
printf (" \n The square root of  %d is: %.2lf", x, result) ;

return 0 ;
}

// defining a function Sqrtfunc()
double  Sqrtfunc ( int n)
{
double Ans ;
// using the sqrt() function to print the square root of the number.
Ans =  sqrt(n) ;
return Ans ;
}
``````

Output 1 In the above-displayed screenshot, the user input is the number 225. The square root for the number 225 is 15.

Output 2 In the above-displayed screenshot, the user input is the number 729. The square root for the number 729 is 27.

## The pow() function in C programming

Calculating the square root of a number is possible with the pow() function too.

Syntax

`int pow(x, 0.5) ;`

Two arguments are required by the pow() function: the first argument defines a variable to calculate the power or square root of the provided value, and the default argument, 0.5, is equal to 1/2 = 0.5. (½)

### 1) Example of pow() function

Let's look at an example utilizing the pow() function to display the square root of a value.

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

int main ()
{
// declare an integer variable
int a ;
double result ;
printf (" Enter the number to get the square root of that number: ") ;
scanf (" %d", &a) ;
// using the pow() function to return the square root of that number.
result = pow(a, 0.5) ;
//Two arguments are required: an input variable and the default value of 0.5.
printf (" \n The square root of %d is: %.2lf", a, result) ;

return 0 ;
}
``````

Output 1 In the above-displayed screenshot, the user input is the number 49. The square root for the number 49 is 7.

Output 2 In the above-displayed screenshot, the user input is the number 36. The square root for the number is 6.36.

2) An example of printing a number's square root without utilizing the sqrt() function with C programming.

``````/* Program for the square root of a number without using the sqrt() function in C programming*/
#include <stdio.h>
int main()
{
// declaration of the variables n, temp, sqrt.
int n ;
float sqrt, temp ;

printf (" Enter the number to get the square root of that number: ") ;
scanf (" %d", &n) ;

// divide the given number n by 2 and store it in the sqrt variable.
sqrt = n / 2 ;
temp = 0 ;

//  Use a while loop to repeatedly check that the variable sqrt and
//variable temp is not the same.
while (sqrt != temp) // sqrt = n and temp is first set to 0.
{
temp = sqrt ; // assigning sqrt value into temp variable

sqrt = ( n / temp + temp) / 2 ;
}

printf (" \n The square root of the number %d is %f", n, sqrt) ;
return 0 ;
}
``````

Output 1 In the above-displayed screenshot, the user input is the number 64. The square root for the number is 8.

Output 2 In the above-displayed screenshot, the user input is the number 3. The square root for the number is 1.732051.

Output 3 In the above-displayed screenshot, the user input is the number 6. The square root for the number is 2.449490.