double l=0, h, m;
int a;
cout << "Input a : ";
cin >> a;
if(a<0)
{
cout << "Error" << endl;
}
else
{
h = (double)a;
m = (l + h) / 2;
int c = 0;
while (c < 1000)
{
if (pow(m, 2) == a)
{
c = 1000;
}
else
{
if (pow(m, 2) > a)
{
h = m;
m = (l + h) / 2;
c++;
}
else
{
l = m;
m = (l + h) / 2;
c++;
}
}
}
}
cout << setprecision(20);
cout << "Square root of " << a << " = " << m << endl;
ไม่มีความคิดเห็น:
แสดงความคิดเห็น