Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Digit Processing: Numbers as Sequences

โญ beginnerโณ 12 min read๐Ÿ“ Lesson 5 of 281

n % 10 and n / 10 peel digits one by one โ€” digit sums, digit counts, digit max, and rebuilding numbers.

Peeling digits with % and /

An integer is a sequence of digits you can process without any string:

long long n; in >> n;
long long digitSum = 0;
while (n > 0) {
    digitSum += n % 10;   // last digit
    n /= 10;              // drop last digit
}

n % 10 = last digit; n / 10 = everything before it. Two operations, every digit problem becomes a loop problem.

The classic digit tasks

| Task | How | | --- | --- | | Digit sum | accumulate n % 10 | | Digit count | count iterations | | Largest digit | track max of n % 10 | | Digit product | accumulate product (beware 0 digits) | | Reverse digits | rev = rev * 10 + n % 10 |

Worked example: is the number a palindrome?

long long n; in >> n;
long long m = n, rev = 0;
while (m > 0) { rev = rev * 10 + m % 10; m /= 10; }
out << (rev == n ? "PALIN" : "NO") << "\n";

Note we kept n untouched in a copy m โ€” the loop destroys what it peels. Forgetting the copy is a classic bug.

Overflow and negatives

Reversing 19-digit numbers can overflow long long โ€” read constraints first. And for negative inputs, decide the convention (usually work with abs(n)) before looping.

โšก Now practice

Ready to Code
Loop Patterns SetSix loop problems: digit work, divisor pairs with the iยทi trick, simulation, and overflow-aware accumulation.
6 challenges ยท ยท ~70 min