square root without math.h | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 Answers
+ 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