遞迴_Summing Digits

 avatar
user_3763047219
c_cpp
a year ago
340 B
0
Indexable
Never
int f(int n);


int f(int n) {
    int sum = 0;
    while (n > 0) {
        sum = sum + n % 10;
        n = n / 10;
    }
    if (sum < 10) {
        return sum;
    }
    else {
        f(sum);
    }
}

#include <stdio.h>


int main() {

    int n;
    scanf("%d", &n);
    printf("%d\n", f(n));
    return 0;

}