c语言从键盘上输入一个4位整数,输出其个位、十位、百位、千位上的数字,并求和。(代码15分,调试1

c语言从键盘上输入一个4位整数,输出其个位、十位、百位、千位上的数字,并求和。(代码15分,调试1c语言

从键盘上输入一个4位整数,输出其个位、十位、百位、千位上的数字,并求和。
代码

#include<stdio.h>
int main()
{int x,a,b,c,d,s;
 scanf("%d",&x);
 a=x/1000;
 b=x/100%10;
 c=x/10%10;
 d=x%10;
 s=a+b+c+d;
 printf("%d %d %d %d\n%d",a,b,c,d,s);
 return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-09-30
#include "stdio.h"
int main(int argc,char *argv[]){
int n,s,t;
printf("Please enter n(int 999<n<10000)...\nn=");
if(scanf("%d",&n)!=1 || n<1000 || n>9999){
printf("Input error, exit...\n");
return 0;
}
for(s=0;n;n/=10){
printf("%d ",t=n%10);
s+=t;
}
printf("\nThe sum of them is %d\n",s);
return 0;
}

运行样例:

相似回答