Algorithm to find square root of a number.

In this guide, we are going to learn programming algorithm to find the square root of a number. Algorithm to find the square root of a number is absolutely a Guesswork.



To improve our algorithm we must start with simple examples. we know that square root of 4 is 2, the square root of 9 is 3 so on.

i.e 4 = 2*2
    9 = 3*3


so we can generate a general formula from above equations .if x is the square root of y .then the general formula is,

x * x = y


Next step to our algorithm is guessing. For example, we want to find the square root of 36. We guess 9 is the square root of 36.Using our general equation, 9*9 = 81 and is greater than 36. So decrease 1 from guessed square root. i.e 8 * 8 = 64 which is also greater than the 36 but closer, generalized.So decrease 1.

Ngeneralized.choose a number x which must be less y which is the number to find the square root.
if the square of x is greater than y,t hen decreases x by 1.Then the x² is less than y increase x by 0.1. Then x² is greater than y increase x by 0.01 and so on until we get the x² = y approx.

The above method is so expensive because of the huge number of iteration. if y is greater than more iteration will be needed. So we must find an alternative method.

For best way, Consider example of 36, the first guess is 9 and its square is 81 which is greater than 36. Now we divide 36 with 9 give 4.And we choose it as second guessing square root. but 4² is less than 36.But we know that the exact square root is in between 4 and 9.

(9 + 4) =6.5


So the next step of the algorithm is to find the average of 4 and 9 .which is to be 6.5. Now take square of 6.5 ,6.5² =42.25 which is greater than 36 .i.e Divide 6.5 into 36.which give 5.53. We can see that 5.53² is less than 36.
Now take an average of previous two guess roots as by previously.

( 6.5 + 5.53 ) = 6.015


Now 6.015² = 36.6025 which has an only slight difference with 36. Repeat the step of averaging until to get the more approx square root. So by this method, we get root with few iteration.
Next step is the generalization of the algorithm. If f1 is the initial guess, in our example f1 = 9. In general case of complementary value (36/9 = 4 ) is y/f1.
Average of initial guess and complementary value give next approx root f2.

f2 = ( f1 + (y/f1 )) / 2

Our f2 = 6.5, f1 .we must repeat the above step, So we assume f2 asLaThe the last

f2 = ( f1 + (y/f1 )) / 2
f1 = f2


The step of our algorithm is termination of the loop. To terminate the loop, we must set a condition which is the difference between f1 and f2 less than some value (such as0.0001). Take y/2 as initial guess which decreases.


Implementation in C

#include <stdio.h>
void main()
	float i, t, c;
	printf("Enter the number :");
	scanf("%f", &c);
	for (t = c, i = 0; i < 100; i++,t = ((t + (c / t)) / 2));
	printf("Sqrt is %f\n", t);

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...