r4 - RECURSION IV Finding square root of a number...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
RECURSION – IV Finding square root of a number Generating Permutations Additive sequence
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Finding Square Root of a number: We can use the Newton’s method, which updates a given guess. Let us take an example. Let the given number be 36. Let us guess that its root is 9. Now since 36/9 is 4, it is certain that the new value of the root has to lie between 9 and 4, as their product equals the given number. So the best way to make the next guess would be to choose a number lying somewhere in the middle of the range (4, 9). This would be ( 4 + 9 )/2, i.e. 6.5 which is more closer to the actual root 6 than the first guess 9 was. We can proceed further by computing the next factor 36/6.5 and then taking the mid value of this factor and 6.5. We see that we very rapidly approach 6, the true value of the root. The steps can be repeated till we find that the current step did not produce appreciable change in the value of the root. Thus given a number x, the first guess could be r = x/2. We keep on generating and testing successive approximations of r until we find the value that is close enough to stop. The updating process may be terminated when | r * r – x | < epsilon. A recursive solution to this problem would need the routine to be called repeatedly with the new value of the root. The stopping condition for the recursive function is already indicated above. #include <stdio.h> double sqroot(double num, double root); int main ( ) { double x,r; printf("\n enter x="); scanf("%lf", &x); r = x/2; r = sqroot(x,r); printf("\n root = %10.2f",r); printf("\n"); } double sqroot(double num, double root)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 7

r4 - RECURSION IV Finding square root of a number...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online