有这个一类数字,他们顺着看和倒着看是相同的数字。例如:121,656,2332等,这样的数字叫做回文数字。编写一个程序,判断从键盘中输入的数字是否为回文数字。
判断回文数字,就是将一个数字,正着计算和倒着计算时,数字的值是相等的,因此需要编写一个函数,计算出数字的回文数字。而一个回文数字的,我们使用的求余运算,然后每次循环乘以10.以获得回文数字。代码如下:
- #include <string.h>
-
#include <stdio.h>
-
-
int isCircle(int n);
-
int reverse(int i);
-
-
int main(int argc, char *argv[])
-
{
-
int n;
-
printf("please input a interget judging is circle:\n");
-
scanf("%d",&n);
-
if(isCircle(n))
-
printf("%d is circle\n",n);
-
else
-
printf("%d is not circle\n",n);
-
-
return 0;
-
}
-
-
int isCircle(int n)
-
{
-
int m;
-
m = reverse(n);
-
if(m == n)
-
return 1;
-
else
-
return 0;
-
}
-
-
int reverse(int i)
-
{
-
int m,j = 0;
-
m = i;
-
while(m)
-
{
-
j = j*10 + m%10;
-
m = m/10;
-
}
-
-
return j;
-
}
peng@ubuntu:~/src/test/c/suanfa/miaoqu$ ./5.6
please input a interget judging is circle:
12
12 is not circle
peng@ubuntu:~/src/test/c/suanfa/miaoqu$ ./5.6
please input a interget judging is circle:
121
121 is circle
阅读(3204) | 评论(0) | 转发(0) |