#include<stdio.h>
//拆分并计算四位数之和
int calculate(int i);
//判断是否为四位数
int isCorrect(int j);
int main() {
int x, y;
scanf("%d", &x);
if (isCorrect(x))
{
y = calculate(x);
printf("%d", y);
}
return 0;
}
int calculate(int i){
int thousand,hundred,ten,one;
int sum;
thousand=i/1000;
hundred=(i%1000)/100;
ten=((i%1000)%100)/10;
one=((i%1000)%100)%10;
sum=thousand+hundred+ten+one;
return sum;
}
int isCorrect(int j){
if(j/1000<1||j/1000>=10){
printf("你输入的不是四位数!");
return 0;
}
else
return 1;
}
运行结果