C语言中判定一个数是否是回文数
注:回文数即数字顺着和反着是同一个数!
看了郝斌老师的C语言视频,固然还只看了80多个,可是照旧有一些体会,编程应该养成精采的编程气势气魄,至少到现 在为止写的这些小措施都应该有下面这样一个进程:
1,流程;(知道措施是凭据奈何的顺序运行的)
2,成果;(领略措施的浸染)
3,试数;(我小我私家简朴的领略为测试进程,把本身看成计较机去执行措施)
/*
2012年4月20日 10:36:23
判定一个数是否是回文数
*/
#include <stdio.h>
int main(void)
{
int val; //存放待判定的数据
int m;
int sum = 0;
printf("请输入您需要判定的数字:\n");
scanf("%d",&val);
m = val;
/*这一块是焦点代码块:
由sum 不绝的从输入的数中获取列位、十位、百位……为了获得反数,需要*10并加上下一位作为个位
sum每更新一次,m值去掉最后一位同时更新
*/
while (m)
{
sum = sum*10 + m%10;
m /= 10;
}
if (sum == val)
printf("Yes!\n");
else
printf("No!\n");
return 0;
}
试数进程(纯粹的体力活)
/*
判定是否是回文数的试数进程
这里只测试焦点块
*/
1234
1 -> m = val = 1234; sum = 0 + 1234%10 = 4; m = 1234/10 =123;
2 -> m = 123; sum = 40 + 123%10 = 40+3 = 43; m = 123/10 = 12;
3 -> m = 12; sum = 430 + 12%10 = 432; m = 12/10 = 1;
4 -> m = 1; sum = 4320 + 1%10 = 4321; m = 1/10 = 0;
false
12321
1 -> m = val = 12321; sum = 0 + 12321%10 = 1; m = 12321/10 = 1232;
2 -> m = 1232; sum = 10 + 1232%10 = 12; m = 1232/10 = 123;
3 -> m = 123; sum = 120 + 123%10 = 123; m = 123/10 = 12;
4 -> m = 12; sum = 1230 + 12%10 = 1232; m = 12/10 = 1;
5 -> m = 1; sum = 12320 + 1%10 = 12321; m = 1/10 = 0;
false
查察本栏目