diff --git a/.vscode/settings.json b/.vscode/settings.json index 32704f3..c0bcd4c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,6 @@ { - "C_Cpp.default.compilerPath": "/usr/bin/gcc" + "C_Cpp.default.compilerPath": "/usr/bin/gcc", + "files.associations": { + "sum_of_digits.h": "c" + } } \ No newline at end of file diff --git a/sum_of_digits.c b/sum_of_digits.c index 29a8347..86af716 100644 --- a/sum_of_digits.c +++ b/sum_of_digits.c @@ -20,12 +20,21 @@ The sum of digits can be calculated as n mod 10 + sum of digits of n / 10 as lon */ -// TODO: the recursive implementation int sum_of_digits_recursive(int n) { - return 0; + if (n == 0) { + return 0; + } + + return n % 10 + sum_of_digits_recursive(n / 10); } -// TODO: the iterative implementation int sum_of_digits_iterative(int n) { - return 0; + int sum = 0; + + while (n > 0) { + sum += n % 10; + n = n / 10; + } + + return sum; } \ No newline at end of file