Problem B: 求各位数字之和
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1363 Solved: 328
[ Submit][ Status][ Web Board]
Description
编写一个程序,计算任意输入的正整数的各位数字之和。(输入的位数不要超过10位。多组测试数据输入)
Input
123
Output
6
Sample Input
145
Sample Output
10
#include<stdio.h> #include<stdlib.h> int main() { long int n; int sum=0; while(~scanf("%ld",&n)) { while(n>0) { sum+=n%10; n=n/10; } printf("%d\n",sum); sum=0; } return 0; }