1、调用库math.h
2、在最开头加入语句:#include
3、平方根函数原形:double sqrt(double x);
第一种:
float i =sqrt (num)
int result = abs (ceil(i)-i) > abs(floor(i)-i) ? (floor(i)) : (ceil(i));
第二种
#include
int my_sqrt(const int);
int
main(void)
{
int x;
printf("Please input a integer:");
scanf("%d", &x);
if (x < 0) {
printf("%d is invalid number\n", x);
exit(1);
}
else
printf("The result is %d\n", my_sqrt(x));
exit(0);
}
int my_sqrt(const int num)
{
int i = 0;
int small;
while (i * i < num)
i++;
small = i - 1;
return (i*i - num > num - small * small ? small : i) ;
}
调用库math.h
也就是在最开头加入语句:#include
平方根函数原形:double sqrt(double x);
1、调用库math.h
2、在最开头加入语句:#include
3、平方根函数原形:double sqrt(double x);
第一种:
float i =sqrt (num)
int result = abs (ceil(i)-i) > abs(floor(i)-i) ? (floor(i)) : (ceil(i));
第二种
#include
int my_sqrt(const int);
int
main(void)
{
int x;
printf("Please input a integer:");
scanf("%d", &x);
if (x < 0) {
printf("%d is invalid number\n", x);
exit(1);
}
else
printf("The result is %d\n", my_sqrt(x));
exit(0);
}
int my_sqrt(const int num)
{
int i = 0;
int small;
while (i * i < num)
i++;
small = i - 1;
return (i*i - num > num - small * small ? small : i) ;
}