## Overview of Square Root in C ** **

In order to serve the business requirements, it becomes necessary sometimes to use mathematical functions in application development. Though some of the basic operations can be performed using simple expressions, it may not be possible to perform advanced expressions without the help of mathematical functions. The advanced mathematical functions include complex functions that are used to solve particular kinds of mathematical problems. There are several mathematical functions available in all the programming languages and it is the same with C language as well. In C programming language we have math.h header file that is used to leverage mathematical functions. Here in this section, we will be learning about finding square root using the C programming language. We will be using math.h header file in order to calculate the square root of any number.

### Logic of Square Root in C

- Before understanding what is square root logic in the C programming language, let’s understand what exactly square root means. The square root is a mathematical jargon. A number is said to be the mathematical square root of any number of multiplying the square root value with itself gives the number for which it was considered square root.
- For instance, the square root of 9 is 3 as 3 multiplied by 3 is nine. The square root is denoted by the symbol √. So if we write √9 then the outcome of this will be 3. The logic works the same way as things work in maths. There are libraries in the programming languages that are used to being the mathematical functionalities into the applications.
- In the C programming language, we will be using maths.h header file that offers various functions that are used to perform the mathematical calculation.
- Coming to the logic that has to be applied in order to get the square root of any number in the C programming language is pretty simple and includes simple mathematical operations. First, we have to validate that the number for which we have to find the square root is not zero or one, if the condition is found negative then the number itself will be the square root as the square root of zero and one is zero and one respectively.

But if the case is found positive we can apply the below logic.

```
Int counter=1,sqroot=1,val=14;
while(sqroot <= val)
```*
* {
counter++;
sqroot = counter*counter;
}
return counter - 1;

- In the above-mentioned logic, first, the value of the counter has been set 1, x stores the value for which we have to find the square root and val stores the value for which we have to find the square root. If the value of Val is less than or equal to the value of sqroot, the statements inside the while loop will be executed. The counter will be increased by one and the value on sqroot will be replaced by the square of the counter.
- The while loop will keep on iterating until the value stored in the sqroot becomes greater than the value stored in val. Once the loop terminates, the value of the counter will be decreased by 1 and will be returned as the square root.
- Please, note that by following this approach we can find the square root in integer data type. We won’t be able to find the floating value of the square root. In order to find the exact square root of any number, we will be using the function provided by the C programming language.

### How to Find Square Root in C?

A c programming language provides us with a platform to use various approaches to find out the square root of any number. We can either draft our own code or can use the predefined function. C to find out the square root. Below is the code that can be used to get the square using a simple mathematical expression. Using the below method will help in getting the square root integer value. For instance, if the square root of any value is 4.965, it will show only 4 as the square root. It will work perfectly fine with the numbers whose square root is an integer. Like the square root of 25 is 5 and the below code will work accurately in order to calculate the square root of such number.

#### Example #1 – Without using the Inbuilt Function

**Code:**

```
#include <stdio.h>
void main()
{
val=9;
if (val == 0 || val == 1)
printf("The square root is %d", val) ;
exit() ;
int counter = 1, sqroot=1, output;
while (sqroot <= val)
{
counter++;
sqroot = counter*counter;
}
output= counter - 1;
printf("The square root is %d", output) ;
}
```

In this program, the user will be getting the output in the integer form as all the variables belong to int datatype. For this example, the output will be 3 as the square root of 9 is 3. If the user opts to find the square root of 38, they will get 6 as output.** **

#### Example #2 – Using Inbuilt Function

**Code:**

```
#include <stdio.h>
#include <math.h>
int main()
{
double val = 87, sqroot;
sqroot = sqrt(val);
printf("The square root of %lf = %lf", val, sqroot);
}
```

In this program, we have used the inbuilt function known as sqrt which is used to find the square root of any number. The output is stored in the double datatype. The outcome of this square root calculation using this program will be 9.327.

**Output:**

### Conclusion

The square root is the mathematical function that can be implemented using the C programming language. The developers can either draft the code to calculate the square root and can also use the inbuilt function to calculate the same. Sqrt is the function provided by C that lets us calculate the square root quickly. Using this function doesn’t take any effort. Not just in C but in every programming language there are inbuilt functions that make development easy and it is a sure thing that they must be having a function to calculate the square root enabling us to leverage the predefined mathematical functions.

### Recommended Articles

This is a guide to Square Root in C. Here we discuss an overview of Square Root in c, logic as well as how to find the Square Root along with an example. You may also look at the following articles to learn more –

3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses