#include
#include
int main()
{
long int n;
char ch[20];
int i,len,sum=0;
scanf("%ld",&n);
if(n<0)
n=-n;
sprintf(ch,"%ld",n);
len=strlen(ch);
for(i=0;isum+=ch[i]-'0';
printf("%d是一个%d位数的整数\n",n,len);
printf("各位数的和= %d\n",sum);
return 0;
}
示例运行结果:
12345678
12345678是一个8位数的整数
各位数的和= 36
#include
int main() {
int i, sum = 0, len = 1;
scanf("%d", &i);
while(i/10 != 0) {
sum += i%10;
len++;
i = i/10;
}
sum+=i%10;
printf("位数:%d,各位之和:%d\n", len, sum);
return 0;
}