Online Compiler C

#include <stdio.h> int digit_sum(unsigned int n) { int sum = 0; if (n == 0) { return 0; } while (n > 0) { sum = sum + (n % 10); n = n / 10; } return sum; } int main(){ printf("%d\n",digit_sum(12305)); return 0; }
This code defines a function `digit_sum` that calculates the sum of the digits of a non-negative integer. It uses a loop to repeatedly extract the last digit using modulo 10, add it to a running total, and then remove that digit by dividing by 10. The `main` function tests it with the number 12305 and prints the result.

- The `if (n == 0) return 0;` check is unnecessary because the `while` loop already handles the case when `n` is 0 by not executing, and `sum` is already initialized to 0. Consider whether this extra condition is needed.
- The function returns `int`, but the sum of digits for a large `unsigned int` could exceed the range of `int`. Think about whether the return type should match the input type to avoid potential overflow.