第五章第七节 更进一步,动手试一试,输入一个3位数,求这个数个位、十位和百位数之和。例如,输入782,输出17。
下面是我写的代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,a,b,c;
printf("输入一个三位数");
scanf("%d",&x);
for(a=x/100%10;b=x/10%10;c=x%10)
printf("%d",a+b+c);
system("pause");
return 0;
}
运行以后会不断重复个位、十位、百位数之和、停不下来。比如输入123,会输出无数个6,是哪里出了问题。
|