QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Sum of Digits

Calculates the sum of the digits of an integer.

  • math
  • digits

Code

// Function to calculate the sum of the digits of an integer
int sum_of_digits(int n) {
    int sum = 0;
    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}

Usage

sum_of_digits(123); // Returns: 6