We help IT Professionals succeed at work.

recursive problem

mikeregas asked
Last Modified: 2012-05-05
I have a recursive program that is suppose to return the square root of a number recursivley, the program will run, but it returns 1. please help
#include <stdio.h>
double square_root(double low, double high);
int main(void)
    float number;
    double root;
    printf("Enter a number to compute the square root of.\n");
    scanf("%f", &number);
    root = square_root(1, number);
    printf("The square root of %.3f is about %.3lf\n", number, root);
    return 0;
	double square_root(double low, double high)
    float mid;
    mid = (high + low) / 2;
    //Base Case
    if((mid*mid) == high)
        return mid;
     //Stops when the high and low numbers are within 0.001 of one another.   
    if((high - low) < 0.001)
        return mid;
    if((mid*mid) > high)
        return square_root(low, mid);
    return square_root(mid, high);

Open in new window

Watch Question

Most Valuable Expert 2014
Top Expert 2015

Are you sure you have the correct recursion?
The method you use looks like it would work better passing 3 numbers.
or else changing


I need to take a number say 19.7 and then it is suppose to return 4.438 is the example that I was given. I know that I am suppose to take two number the low which would be 0 and the high and which would be 19.7 and recursively bring it down to an approximate square root. So there would be a third number in the equation the  mid.
Top Expert 2006

>square_root(1, number);
second arg is the number whose sq root you want.

Your recursive calls are
>return square_root(low, mid);<<<------ you are calculating sq root of mid!!
>return square_root(mid, high);

Hello mikergas,

You are using "Babylonian method" to calculate square root. You are not using it in right way.
For proer desciption of babylonian method can be found in link

I am attaching modified and working version of your code..
<code edited out>

Open in new window

I will take care in future


I am confused on what is right in my code and what is wrong? can you please give me some direction to go in. my code is return a 1 for any number that I input and I know that is worng.
Top Expert 2006

http:#22647118 lists your problem
http:#22646982 hints at a solution
According to algorithm
x(n+1) = 1/2( x(n) + a / x(n) )

where a is the number for which square root should be calculated.
Change calculation of 'mid' to above formula.

Also, since you are always calling function recursively with low as 1, your answer is always converging to 1.


x(n+1) = 1/2( x(n) + a / x(n) ) let me make sure that I understand this. if a is the number that I am trying to get the root of then x is a function of what? I am truely lost with recursion
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.