Sum of Digits: flowchart, algorithm and program

This program reads an integer and prints the sum of its digits. It peels off the last digit with n % 10, adds it to sum, then drops that digit with n / 10 until nothing is left.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Set sum = 0.
  3. Step 3: Read n.
  4. Step 4: If n < 0, go to step 5; otherwise go to step 6.
  5. Step 5: Set n = -n.
  6. Step 6: If n > 0, go to step 7; otherwise go to step 10.
  7. Step 7: Set digit = n % 10.
  8. Step 8: Set sum = sum + digit.
  9. Step 9: Set n = n / 10. Go to step 6.
  10. Step 10: Print sum.
  11. Step 11: Stop.

Pseudocode

BEGIN
    sum ← 0
    READ n
    IF n < 0 THEN
        n ← -n
    END IF
    WHILE n > 0 DO
        digit ← n % 10
        sum ← sum + digit
        n ← n / 10
    END WHILE
    PRINT sum
END

Program in C, Python and Java

Sum of Digits program in C

#include <stdio.h>

int main() {
    int n, digit, sum = 0;
    printf("Enter n: ");
    scanf("%d", &n);
    if (n < 0)
        n = -n;
    while (n > 0) {
        digit = n % 10;
        sum = sum + digit;
        n = n / 10;
    }
    printf("Sum of digits = %d\n", sum);
    return 0;
}

Sum of Digits program in Python

n = int(input("Enter n: "))
sum = 0
if n < 0:
    n = -n
while n > 0:
    digit = n % 10
    sum = sum + digit
    n = n // 10
print("Sum of digits =", sum)

Sum of Digits program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n, digit, sum = 0;
        System.out.print("Enter n: ");
        n = sc.nextInt();
        if (n < 0)
            n = -n;
        while (n > 0) {
            digit = n % 10;
            sum = sum + digit;
            n = n / 10;
        }
        System.out.println("Sum of digits = " + sum);
    }
}

Sample input: n = 4825
Output: Sum of digits = 19

How it works

  • sum starts at 0. If n is negative, n = -n makes it positive so the loop works on the digits only.
  • digit = n % 10 gives the last digit of n. For 4825 that is 5.
  • sum = sum + digit adds that digit to the running total.
  • n = n / 10 uses integer division to remove the last digit, so 4825 becomes 482.
  • The while (n > 0) loop repeats these steps until n becomes 0. For 4825 the sums go 5, 7, 15, 19, so the output is 19.

Dry run (trace table)

StepLineBoxsumndigitOutput
13Start
24sum = 00
36Read n04825
47n < 0?04825
59n > 0?04825
610digit = n % 10048255
711sum = sum + digit548255
812n = n / 1054825
99n > 0?54825
1010digit = n % 1054822
1111sum = sum + digit74822
1212n = n / 107482
139n > 0?7482
1410digit = n % 107488
1511sum = sum + digit15488
1612n = n / 101548
179n > 0?1548
1810digit = n % 101544
1911sum = sum + digit1944
2012n = n / 101904

Time and space complexity

Time: O(log n) · Space: O(1). The loop runs once per decimal digit of n (about log10 n times) and uses only a few variables.

Common mistakes

  • Forgetting to initialize sum = 0, so the result starts from a garbage value.
  • Forgetting n = n / 10 inside the loop, which makes it run forever because n never changes.
  • Not handling negative input: with n < 0 the loop condition n > 0 is false at once and the sum prints as 0.

Viva questions

Q1. Why is sum initialised to 0 and not 1?

Because sum is an accumulator for addition, and 0 is the identity for addition. Starting at 1 would make every answer one too large.

Q2. What does n % 10 do in this program, and what does n / 10 do?

n % 10 gives the remainder on dividing by 10, which is the last digit. n / 10 is integer division, so it drops the last digit and the number gets shorter by one place.

Q3. Why does the program write if (n < 0) n = -n; before the loop?

The loop condition is n > 0, so a negative number would never enter the loop and the sum would wrongly be 0. Making n positive first means the digits of a negative input are still added.

Q4. What is the output if the user enters 0? What if the input is -123?

For 0 the loop never runs, so the sum stays 0, which is correct. For -123 the sign is removed first, so the digits 1+2+3 give 6.

Q5. Which type of loop is used here, and why a while loop rather than a for loop?

A while loop, because we don't know in advance how many digits there are. We repeat until n becomes 0, and a condition-controlled loop suits that.

Q6. What is the time complexity of this program?

O(d), where d is the number of digits in n, which is the same as O(log10 n). Each pass removes exactly one digit.

Q7. How would this fail for the input -2147483648, and why?

On a 32-bit int, +2147483648 doesn't fit, so -n overflows. That is undefined behaviour, and the result may stay negative so the loop is skipped. Using long long or unsigned avoids it.

Q8. What would you change to find the digital root, or to count the digits instead of summing them?

To count digits, replace sum = sum + digit with a counter that goes up by 1 each pass. For the digital root, repeat the whole digit-sum process until the result has a single digit, adding an outer loop.

Frequently asked questions

Why do we use n % 10 and n / 10 to find digits?

n % 10 gives the remainder after dividing by 10, which is the last digit. n / 10 with integers throws away that last digit, so the next digit becomes the last one.

What does this program print for 0 or a negative number?

For 0 the loop never runs and it prints 0. For a negative number like -4825, the program first makes it positive, so it prints 19.

Can I find the sum of digits without a loop?

Yes, with recursion: sumDigits(n) = n % 10 + sumDigits(n / 10), stopping when n is 0. You could also convert the number to a string and add each character, but the loop is simplest.

More programs

← All programs

Try AI Flowchart free →