Playing with Digits
Extract, count, sum, and reverse digits — essential tricks for number-based problems!
Open interactive version (quiz + challenge)Real-world analogy
What is it?
Playing with digits means breaking a number into its individual digits and performing operations on them. The two key operations are: n % 10 (extract the last digit) and n / 10 (remove the last digit). By repeating these in a loop, you can count digits, sum them, reverse the number, check for palindromes, and solve many number theory problems in competitive programming.
Real-world relevance
Digit manipulation shows up everywhere: credit card validation (the Luhn algorithm sums digits), ISBN book number verification, digital clocks, odometers in cars, and many CP problems. When an ATM counts money or a calculator displays a result, digit manipulation is happening behind the scenes.
Key points
- Extract the last digit: n % 10 — The modulo operator with 10 gives you the last digit of any number. 1234 % 10 = 4, 507 % 10 = 7, 90 % 10 = 0. This is because dividing by 10 and taking the remainder always gives the ones digit.
- Remove the last digit: n / 10 — Integer division by 10 chops off the last digit. 1234 / 10 = 123, 507 / 10 = 50, 9 / 10 = 0. When n becomes 0, you have processed all digits!
- Count digits in a number — Loop and divide by 10 until the number becomes 0: int count = 0; while(n > 0) { count++; n /= 10; } For 1234: count goes 1→2→3→4 as n goes 123→12→1→0. Edge case: if n is 0, the count should be 1!
- Sum of digits — Extract each digit with %10 and add it: int sum = 0; while(n > 0) { sum += n % 10; n /= 10; } For 1234: sum = 4+3+2+1 = 10. This is used in many contest problems, especially those about divisibility.
- Reverse a number — Build the reversed number digit by digit: int rev = 0; while(n > 0) { rev = rev * 10 + n % 10; n /= 10; } For 1234: rev goes 4→43→432→4321. The key trick is rev * 10 shifts digits left, then + n%10 adds the new digit.
- Check if a number is a palindrome — A number palindrome reads the same forwards and backwards (like 12321 or 1001). Simply reverse the number and compare: if(original == reversed) it is a palindrome! Store the original value before reversing since the loop destroys n.
- Digital root — Keep summing digits until you get a single digit. For 9875: 9+8+7+5=29, then 2+9=11, then 1+1=2. The digital root is 2. A mathematical shortcut: digital root = n % 9 (if result is 0 and n is not 0, it is 9). Cool, right?
- Working with individual digits — Sometimes you need to store digits in an array. Extract them with %10 and /10, push into a vector, then reverse it (since they come out in reverse order). Or convert the number to a string: string s = to_string(n); and work with characters!
Code example
#include <bits/stdc++.h>
using namespace std;
// Count digits in a number
int countDigits(int n) {
if (n == 0) return 1;
int count = 0;
while (n > 0) {
count++;
n /= 10;
}
return count;
}
// Sum of digits
int digitSum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
// Reverse a number
int reverseNum(int n) {
int rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}
// Check if number is palindrome
bool isPalindromeNum(int n) {
return n == reverseNum(n);
}
int main() {
int n;
cin >> n;
cout << "Number: " << n << endl;
cout << "Digits: " << countDigits(n) << endl;
cout << "Digit sum: " << digitSum(n) << endl;
cout << "Reversed: " << reverseNum(n) << endl;
if (isPalindromeNum(n)) {
cout << n << " is a palindrome!" << endl;
} else {
cout << n << " is NOT a palindrome." << endl;
}
// Bonus: extract all digits into a vector
vector<int> digits;
int temp = n;
while (temp > 0) {
digits.push_back(temp % 10);
temp /= 10;
}
reverse(digits.begin(), digits.end());
cout << "Individual digits: ";
for (int d : digits) cout << d << " ";
cout << endl;
return 0;
}Line-by-line walkthrough
- 1. while (n > 0) { count++; n /= 10; } — Each time we divide n by 10, we remove one digit and increment our counter. When n becomes 0, we have counted all digits. We handle n=0 specially because the loop would not execute.
- 2. sum += n % 10; n /= 10; — First we grab the last digit (n%10) and add it to sum. Then we remove that digit (n/=10). Repeat until n is 0. For 1234: we get 4, then 3, then 2, then 1.
- 3. rev = rev * 10 + n % 10; — This is the clever digit-building formula. rev*10 shifts all existing digits one place left (making room for a new digit on the right). Then + n%10 places the extracted digit in that new spot. For 1234: rev goes 0→4→43→432→4321.
- 4. return n == reverseNum(n); — To check palindrome, we reverse the number and compare with the original. If they are equal, the number reads the same both ways. Clean and simple!
- 5. digits.push_back(temp % 10); — We extract digits one by one and store them in a vector. They come out in reverse order (4,3,2,1 for 1234), so we reverse the vector at the end to get them in the right order (1,2,3,4).
Spot the bug
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
// Try to count digits and sum them
int count = 0, sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
count++;
}
// Now try to reverse n
int rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
cout << "Digits: " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Reversed: " << rev << endl;
return 0;
}Need a hint?
Show answer
Explain like I'm 5
Fun fact
Hands-on challenge
More resources
- Digit Manipulation in C++ (YouTube)
- Digit Problems — GeeksforGeeks (GeeksforGeeks)
- Number Digit Problems (Codeforces)