各位数字和.c 229 B

123456789101112131415161718192021
  1. #include <stdio.h>
  2. int sum(int x) {
  3. int sum;
  4. while(x > 0) {
  5. int digit = x % 10;
  6. sum += digit;
  7. x /= 10;
  8. }
  9. return sum;
  10. }
  11. int main() {
  12. int x;
  13. scanf("%d",&x);
  14. int result = sum(x);
  15. printf("%d",result);
  16. return 0;
  17. }