Square Root in C
Introduction
The square root of a number can be thought of as a number that is when multiplied by itself (squared), gives a value equal to the original number.
For example, the square root of 9 is 3 as 3*3=9. For 16, it is 4 as 4*4=16.
Let’s discuss how to find the square root of a number in C
You can also read about dynamic array in c.
Implementing function for finding the square root
You know that if the square root of ‘n’ is ‘i’, then i*i=n. So using this concept, we try to implement a function. Before that, you should know about perfect squares. A perfect square is a number that has its square root as an integer. Few perfect squares are 4, 9,16, 25 as their square roots are 2, 3, 4, and 5, respectively, which are integer values.
A simple program that can help in calculating the square root of a number is as follows:
#include <stdio.h>
#include <math.h>
double square_root(double val)
{
double ans = 1, sqr = 1, i = 1;
while (sqr <= val) //checking if squares of the numbers from 1 till given value is smaller than the number
{
i++;
sqr = i * i;
}
ans = i - 1;
return ans;
}
int main()
{
double n;
scanf("%lf", &n);
printf("The square root of %d is %lf", (int) n, square_root(n));
return 0;
}
This is a straightforward program. Let’s try and run this program.
Input 1:
9
Output 1:
The square root of 9 is 3.000000
Input 2:
10
Output 2:
The square root of 10 is 3.000000
So, we observe that this function returns a float value and gives the right output only when the input is a perfect square. For others, it produces an incorrect answer. Let’s now try to make it work for all the numbers.
One way could be to consider the decimals between the integers on the number line as well.
But, again, between 1.1 and 1.2, there exist 1.11,1.12,1.13,........ So it needs to be decided to what precision we want to go.
The basic technique that we will use is based on Binary Search. If you are not aware of it, you can refer to this article to have a complete understanding of the algorithm we are going to discuss further.
So, one observation that you might have made is that the square root of a number is always less than the number, and we always express the square roots as positive numbers. TO generalize, 0 ≤ √N ≤ N, where N is any number. The square root will have an integral part and a decimal part.
Let’s first figure out the integral part. In this methodology, we first check the number which lies in the middle of 0 and N, i.e., N/2. If it is the square root, then we stop as this number is the square root, otherwise, we see if we have to go to left or right, depending on if the square of (N/2) is greater or less than the N, respectively. Similar to how we do in binary search.
After the integral part has been fixed, we move to find the fractional part. First, we need to decide how precise we want the value to be. Let’s keep it 3 places after the decimal for this article. Now we increment an integral part with a specific very small value, and each time we do it, we are fixing the places in the fractional part.
After every loop of increment, we change the increment value to 1/10th of what it earlier was, to gain more precision. We will run this for 3 loops as we need a precision of 3 places after the decimal.
Let’s see the function for this in C and try to implement it on Online C compiler.
#include <stdio.h>
float square_root(int val)
{
int left = 0, right = val, mid;
float sqrt;
while (left <= right) //finding the integral part
{
mid = (left + right) / 2;
if (mid *mid == val)
{
sqrt = mid;
break;
}
if (mid * mid < val)
{
sqrt = left;
left = mid + 1;
}
else
{
right = mid - 1;
}
}
float inc_val = 0.1;
for (int i = 0; i < 3; i++) ///finding the fractional part
{
while (sqrt * sqrt <= val)
{
sqrt += inc_val;
}
sqrt = sqrt - inc_val;
inc_val = inc_val / 10;
}
return sqrt;
}
int main()
{
int n = 12;
scanf("%d", &n);
printf("The square root of %d is approximately %0.3f", n, square_root(n));
return 0;
}
Input 1:
9
Output 1:
The square root of 9 is approximately 3.000
Input 2:
12
Output 2:
The square root of 12 is approximately 3.464
While printing, you should set the precision by adding 0.3 in the format specifier of float—something like this: “%0.3f” as we need only three digits after the decimal.
In the above outputs, the square root of 11 is approximately 3.316, which is slightly less precise but can lead to severe destruction (You can read about the Patriot Missile mishap later).
So, the accuracy can be increased if we consider calculating more values than what we have done after the decimal. Then you should also consider using double as a data type for more accuracy.
Another way can be by increasing the number of digits after decimal from 3 to 5, 6, 7 till wherever required.
Well, that’s for you to try. But now you have an idea of how to implement such an algorithm.
Let’s move to learn about the built-in functions that C provides for ease and efficiency while writing code with no hassle of figuring out what increment value we should use and other stuff.
Must Read, Binary to Hex Converter.
Built-in functions in C to find the square root
There are mainly two ways that you can employ to calculate the square root of a number. Both of these built-in functions are present in the ‘math’ library. So, it needs to include whenever you want to use those functions like this:
#include<math.h>
These functions are: sqrt() and pow()
sqrt() function
This function simply takes a value as a parameter and produces its square root as output. The return type of sqrt() function is double. Also, the value it takes as a parameter is implicitly converted to double data type first if it is not already.
Syntax: double sqrt(double parameter)
For example:
#include<stdio.h>
#include<math.h>
void builtin_sqrt(int val)
{
printf("The square root of %d is %lf",val,sqrt(val));
}
int main()
{
int n;
scanf("%d",&n);
builtin_sqrt(n);
return 0;
}
Input 1:
25
Output 1:
The square root of 25 is 5.000000
Input 2:
26
Output 2:
The square root of 26 is 5.099020
This function has variations like sqrtf() and sqrtl() for returning and taking as parameters the values of float and long double data types, respectively.
pow() function
This function is basically used to find the power of a number like 5^{2}, 6^{3}, etc. But, if you see, the square is also a kind of exponent, which is 0.5. So, it can be used to find the square root as well.
This function returns a value of double and takes two parameters. These parameters are also of double data type. If not already, while inputting, they are implicitly converted.
Syntax: double pow(double base, double power)
For example:
#include<stdio.h>
#include<math.h>
void builtin_pow(int base)
{
printf("The sqaure root of %d is %lf",base,pow(base,0.5));
}
int main()
{
int n;
scanf("%d",&n);
builtin_pow(n);
return 0;
}
Input 1:
36
Output 1:
The square root of 36 is 6.000000
Input 2:
50
Output 2:
The square root of 50 is 7.07168
You can practice by yourself on online compiler.
FAQs
- Name some built-in functions to find the square root in C.
sqrt() and pow() are the built-in functions for it.
- In which library are the built-in functions to find the square root present in C?
They are available in math.h header file.
- What is the return type of pow()
The return type of the pow() function is double.
Key Takeaways
This article explains how to calculate the square root of a number in C language in detail.
We hope that this blog has helped you enhance your knowledge about Square Root in C, and if you would like to learn more, check out our articles on the C language. Do upvote our blog to help other ninjas grow.
Happy Coding!