square root algorithm???

This is a discussion on square root algorithm??? within the C++ Programming forums, part of the General Programming Boards category; I am building a calculator and already have addition, subtraction, multiplication, division, squaring a number, and I want to add ...

square root algorithm???

I am building a calculator and already have addition, subtraction, multiplication, division, squaring a number, and I want to add in the square root function. The only problem is I don't know what the formula or whatever its called is for finding the square root of a number. Does anyone know it? I figured out if you could multiply 64 by .125 and it gives you 8 but that doesn't work for all numbers such as 49. So if someone could tell me the formula I would be greatly appreciated. Thanks alot.

In my c++ class we basically did a binary search for the number. You use a precision const to let you know when you are close enough.

You set your high mark (the number you are looking for) and your low mark (1). If the number is 0<x<1 then you switch it so the high is 1 and the low is the number

Take the mid point. Square it. If its within your precision to the number you are looking for use that. If not: If its too large set your new high to be that mid point. If too low set your low mark to that mid point. continue

How can I turn that into something the calculator can perform? I want it to be so the user can input a number and it tells the answer. But I can't think of any sort of set formula. I did some research on google, but I got basically what you said about the guess and estimate etc. way. But the only way to get that to work would be to make the computers first guess to always be 10 or some number then it return if it was true or not and if so let that be the answer and if not to return it was higher or lower. If it was higher take half of that number like you did and repeat until it gives me only an estimate. But that would take a crapload of code. I was wondering if any of you had like a set formula of how to do it or if the estimating is the only way. How does a regular calculator do it. Like the computer calculator.

I don't quite understand how to put what you said lucky into a loop. I had already written this before I read your post, but it doesn't work right. I was wondering if this could work and if it could whats wrong with it. If not could you give a brief description of what I need to do for the loop. This square root this is basically me trying to learn loops better. But anyways here is the code I wrote.

It compiles fine, but all it does is ask for the number and when I input a number such as 49 it just goes straight to "Press any key to continue..." and then it exits. I'm a little confused can someone help me out.

To be clear, you are not implementing the algorithm Sang-drax and I spoke of. Your problem is one of logic. Once you reach theAnswer = 7 in your first if statement the while test will evaluate to false and the loop will end without ever printing anything. Just remove the else from your third if statement so it will check every time through if you've found the right number.

You should understand that this will work only for any number that has an even square root. If not you will just loop forever.

That works great. Thanks. The next thing I want to do is have the program keep repeating after it does a problem. I want to enter an exit choice that when you choose it will exit, but otherwise the program will repeat. So after you pick square root and you find the square root of 49 is 7 it will ask "What would you like to do?"And present the menu again. I figure it is a loop but for I loop the whole thing or what?

I got it. I add a Do...While loop in there. I added int loopcount. So I have everything defined and then my code is surrounded by a do....while (loopcount > 0); and inside my code I never stated to increase or decrease my loopcount so it should run infinitly. But when I hit the exit number it still exits immediately. So it is all worked out. Thanks for all the help.