Home » Square Root in C

Square Root in C

This section will discuss finding the square root of a given number using the sqrt() function in the C programming language. In mathematics, the square root of a number is just the reverse of squaring of that number. It means when the same is multiplied by itself to return the square of a number. And the single number multiplied by itself is called the square root of a number. For example, suppose we want to get the square of any number is 3, the number 3 is multiplied by itself as 3 * 3 to return the square 9. And the same number 3 is called the square root of the number 9. Similarly, we have a number 81, and the square root of that number is 9 (9 * 9 = 81).

Square Root in C

In C programming, the sqrt() function is a pre-defined library function used to calculate the square root of a number. The sqrt() function is defined in the math.h header file. So, we need to write the <math.h> header file while using the sqrt() function in C. Furthermore, we can find the square root of the given number without using the sqrt function.

Syntax of the sqrt() function

In the above syntax, the sqrt() function takes a single argument as double to return its square root in double data types.

arg: It is a double data type argument of the sqrt() function.

Return value: The sqrt function returns a square root of the given number in a defined double data type.

Note: We can find the square root of the int, float, double or long double data type number by explicitly converting the given data type to another.

Algorithm to find the Square Root

  1. Declare an integer variable, as num.
  2. Use the sqrt() function to pass the num variable as an argument to find the square root.
  3. Print the result.
  4. Exit or terminate the program.

Example 1: Program to get the square root of a number using the sqrt() function

Let’s consider an example to calculate the square root of the given number using the sqrt() function in C.

Output:

The square root of 289 is: 17  The square root of 12.25 is: 3.50  The square root of 144.00 is: 12.00  

Example 2: Program to take a number from user and to get the square root

Let’s consider an example to print the square root of a number by taking an input from the user and then use the sqrt() function in C.

Output:

Enter any number to get the square root: 625  The square root of 625 is: 25.00   

Example 3: Program to find the square root using user defined function

Let’s create a program to get the square root of the given number using the user defined function in the C programming language.

Output:

Enter any number to get the square root: 87  The square root of 87 is: 9.33  

pow() function

The pow() is a pre-defined function of math.h header file for calculating the power of a given number.

Syntax of the pow() function

The pow() function takes two arguments: the first argument defines a variable to obtain the power, or the square root of the given number, and 0.5 is a default argument that is equal to ½ or 1 / 2 = 0.5.

Example 4: Program to get the square root of a number using the pow() function

Let’s consider an example to print the square root of a number using the pow() function in C.

Output:

Enter any number to get the square root: 1225  The square root of 1225 is: 35.00  

In the above program, we take the integer value of variable x is 1225 from the user and pass x as an argument to the pow() function to return the power or square root of the given number.

Example 5: Program to get the square root of a number without using the sqrt() function

Let’s consider an example to print the square root of a number without using the pre-defined sqrt() function in C.

Output:

Enter a number to get the square root: 2  The square root of 2 is 1.414214  

In the above program, we input a number from the user which we find the square root. So, first, we divide the given number by 2 and store it in the sqrt variable. After that, we initialize temp with 0. And then use the while loop that continuously iterates and checks sqrt is not equal to the temp, and on each iteration, it assigns the sqrt value to temp, and the sqrt gets a new value by solving the logic (num/temp + temp) /2; And then prints the square root of 2 is 1.414214.


You may also like