#include
#define ABS(X) X<0?-X:X
void main()
{
int x,sum=0;
printf("input x=");
scanf("%d",&x);
x=ABS(x);
while (x>0)
{
sum+=x%10;
x/=10;
}
printf("%d\n",sum);
}
碰巧我也做这个题,绝对是正确的,TC2.0验证。
main()
{
int c,s=0,k;
printf("input a number:");
scanf("%d",&k);
c=k;
for(;c;)
{
s=s+c%10;
c=c/10;
}
printf("%d各位数字之和为:%d",k,s);
}
告诉你核心代码
int x,i=0;
...
...
...
while(x!=0)
{i+=abs(x)%10;
number/=10;
}
printf("%d",i);
别的自己写 绝对没问题
#include
#define ABS(X) ((X)<0?-1*(X):(X))
int main(void)
{
int x,sum=0;
scanf("%d",&x);
x=ABS(x);
while (x>0){
sum+=x%10;
x/=10;}
printf("%d\n",sum);
return 0;
}