To add the digits of 273, to reverse it or to count how many digits it has, a program must first separate 2, 7 and 3. A number is stored as a single value, not as a row of digits, so the digits have to be worked out by arithmetic.

The Two Key Operations

Two integer operations do all the work.

  • n % 10 gives the last digit of n, because it is the remainder when n is divided by 10.
  • n / 10 removes the last digit, because integer division discards the remainder.

For n = 273, 273 % 10 is 3 and 273 / 10 is 27.

Definition: Digit extraction. The process of separating the individual digits of an integer by repeated use of the remainder and integer-division operations.

Extracting from Right to Left

Repeating the two operations until the number becomes 0 yields the digits from right to left.

Table 3.1: Trace of right-to-left extraction for n = 273

Pass n before n % 10 (digit) n / 10 (n after)
1 273 3 27
2 27 7 2
3 2 2 0

The loop makes one pass for each digit, so counting the passes gives the number of digits. Adding the digit to a running total on each pass gives the sum of the digits.

Extracting from Left to Right

The leftmost digit is obtained by dividing by the place value of that digit. For 273, the place value is 100: 273 / 100 is 2, and 273 % 100 leaves 73. The place value is then divided by 10 and the step is repeated.

The method has two stages.

  1. Find the largest power of 10 that does not exceed n. Start with p = 1 and multiply p by 10 while n / p >= 10.
  2. While p > 0: display n / p, set n = n % p, and set p = p / 10.

Special Cases

The number 0 has one digit, but the loop while (n > 0) makes no pass for it. Zero must therefore be handled separately, or a do-while loop must be used. A negative number is first converted to its positive value, since the sign is not a digit.

Example 3.1: Sum and count of digits

Problem: Read a positive integer and display its digits from right to left, together with the number of digits and their sum.

Solution:

  1. Take the last digit with % 10, then remove it with / 10.
  2. Add each digit to sum and add 1 to count.
  3. Repeat until the number becomes 0.
#include <stdio.h>

int main(void)
{
    int n, digit, sum = 0, count = 0;

    printf("Enter a positive integer: ");
    scanf("%d", &n);
    printf("\nDigits from the right: ");
    while (n > 0)
    {
        digit = n % 10;
        printf("%d ", digit);
        sum = sum + digit;
        count++;
        n = n / 10;
    }
    printf("\nNumber of digits = %d\n", count);
    printf("Sum of digits    = %d\n", sum);
    return 0;
}

Input:

273

Output:

Enter a positive integer:
Digits from the right: 3 7 2
Number of digits = 3
Sum of digits    = 12

Result: The digits appear as 3, 7, 2. There are 3 digits and their sum is 2 + 7 + 3 = 12. The variable n is 0 when the loop ends, so the original number is lost unless a copy was kept.

Common mistakes

  • Forgetting that the loop destroys n; a copy must be kept if the original value is needed later.
  • Expecting the digits in left-to-right order from the % 10 method.
  • Overlooking the input 0, for which a while (n > 0) loop makes no pass.

Key takeaways

  • n % 10 gives the last digit and n / 10 removes it.
  • Repeating these until n is 0 gives the digits from right to left.
  • Left-to-right extraction divides by the place value of the leading digit.
  • One pass is made per digit, so the pass count is the digit count.

Practice questions

  1. What are the values of 4096 % 10 and 4096 / 10?
  2. How many passes does the extraction loop make for the number 50321?
  3. Which place value is used first when extracting the digits of 8254 from the left?

Answers to the practice questions

  1. 4096 % 10 is 6 and 4096 / 10 is 409.
  2. Five, one for each digit.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit III