2007/11/30

[C++] Finding Square Root without using sqrt()

不用 sqrt 函式自己寫開根號的 code

http://www.dreamincode.net/code/snippet244.htm

[cpp]
/* Code written by Sanchit Karve
A.K.A born2c0de
Contact Me at born2c0de AT hotmail.com

20 August, 2005

*/




#include
#include

using namespace std;


float sqroot(float m)
{
float i=0;
float x1,x2;
while( (i*i) <= m )
i+=0.1;
x1=i;
for(int j=0;j<10;j++)
{
x2=m;
x2/=x1;
x2+=x1;
x2/=2;
x1=x2;
}
return x2;
}

int main()
{
cout << "Enter a Number:";
int no;
cin >> no;
cout << "Square Root using sqroot()= " << sqroot(no) < << "Square Root using sqrt() = " << sqrt(no);

return 0;
}

[/cpp]

No comments:

Post a Comment