0
square root without math.h
Hi, i'm making an algorithm which give the square root of a number. The problem is that it gime te square root but with a decimal extra. Example: the square root of 4, 2.1. #include <iostream> using namespace std; int main() { float n1,i=0,res; cout<<"Digite un numero para hallar su raíz cuadrada: "; cin>>n1; do{ //Many decimals for more precision. i=i+0.00001; res=i*i; } while(res<=n1); cout<<endl<<"La raíz cuadrada es: "<<i; return 0; }
2 ответов
+ 3
You can typecast i to integer. That way decimal will be removed.
cout << (int)i << endl;
+ 1
Look for Newton-Raphson if you need a more effective approach to determine a square root.