方法一:
#include<stdio.h>
int main()
{
//unsigned long long的最大值:1844674407370955161
int result=0;
unsigned long long integer=0;
unsigned long long n=1;
printf("input a integer:");
scanf("%lld",&integer);
do
{
result+=(integer/n)%10;
n*=10;
}while(1<integer/n);
result+=integer/n;
printf("result:%d",result);
return 0;
}
方法二:
#include<stdio.h>
#include<string.h>//strlen
int main()
{
char str[256];
int result=0;
int i=0;
printf("input a integer:");
scanf("%s",str);
for(;i<strlen(str);++i)
{
result+=str[i]-'0';
}
printf("result:%d\n",result);
return 0;
}