I've been messing about with some programs to calculate some basic math for me, but i've hit a brick wall with Square Root. Obviously, i could just use the sqrt(); from cmath, but i want to try and do it manually using some kind of formula or method rather than just using a premade function.

I've been browsing around for a formula for computing sqrt, and Wikipedia has a couple.

http://en.wikipedia.org/wiki/Square_Root

They're kind of complicated and i have a hard time understanding most of them. I did try the one with the logarithm,sqrt(x) = 10.5^log(x), but that gave me mostly random results, and definitely not the sqrt.

What i have now is not the prettiest way of doing it. This program actually succeeds in trying every combination of digits until it hits the correct one, but it only works with integers, so if the input to the program isn't something like 9 or 16, it just keeps on calculating numbers forever.

A very ugly way of doing it - i know. Is there a simple formula i have missed? How does the sqrt(); in cmath do it? I need it to work with non-integers aswell. As you might have noticed, im not very good at maths and long formulas, please be gentleCode:#include <iostream> #include "clscr.h" float Square(float a); int main() { float a = 0, b = 0; std::cout << "Square Root (a)" << std::endl; std::cout << "Enter a: "; std::cin >> a; clscr(); b = Square(a); std::cout << "Square Root (" << a << ") = " << b << std::endl; std::cin.ignore(2); } float Square(float a) { int i = 0; float b = 1; while (1) { if ((b*b) == a) { return(b); } else { b++; } } }