本题解法
从键盘输入一个正整数n,计算该数的各位数字之和并输出。例如输入5246,计算5+2+4+6=17并输出#include <stdio.h> int main() { int count = 0; for(int n = '0'; n != '\n'; n = getchar()) count += n - '0'; printf("%d\n", count); return 0; }
#include<stdio.h> int main() { int n,sum=0; scanf("%d",&n); while(n) { sum += n%10; n/=10; } printf("%d\n",sum); return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { int theNum = 0; scanf("%d", &theNum); int theResult = 0; while (theResult += (theNum%10),theNum /= 10) {} printf("%d\n", theResult); }
unsigned binary_ascii_add(unsigned value) { unsigned quotient; unsigned sum = 0; quotient = value / 10; if (0 != quotient) { sum += binary_ascii_add(quotient); } sum += value % 10; return sum; }