自定义求平方根,请问大佬还能否改进???
#include <stdio.h>int main()
{
int sqrt2(int a);
int x,y;
printf("请输入一个数:");
scanf("%d",&x);
y = sqrt2(x);
if(y == 0)
{
printf("The data is Error!!!\n");
}
else
printf("%d\n",y);
return 0;
}
int sqrt2(int a)
{
int n,sum = 0;
for(n = 0;n < a/2;n++)
{
sum = n*n;
if(sum == a)
{
return(n);
}
else
break;
}
return(0);
}
页:
[1]