square root without math.h | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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; }

12th Sep 2019, 3:29 PM
Nahuel Alejandro CƔceres
Nahuel Alejandro CƔceres - avatar
2 Respostas
+ 3
You can typecast i to integer. That way decimal will be removed. cout << (int)i << endl;
12th Sep 2019, 3:49 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 1
Look for Newton-Raphson if you need a more effective approach to determine a square root.
12th Sep 2019, 4:33 PM
Thoq!
Thoq! - avatar