- Why "single-digit" is best defined as "digit count == 1", not as a range check
- How the do-while loop counts digits without destroying the original value
- Why a range check like
n >= 0 && n <= 9misses negative single-digit numbers - Why zero is a single-digit number and how the do-while handles it naturally
- How this pattern extends to "is this a two-digit number?", "is this a perfect square?" and similar checks
What "Single-Digit" Really Means
A single-digit number is one that can be written with exactly one decimal digit — that is, one of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, or their negative counterparts −0 (same as 0), −1, …, −9. That's it. Everything else — 10, 999, −42, 100000 — is not single-digit.
The naive approach is a range check: if (n >= 0 && n <= 9). That works for positive integers, but it fails for negatives. Try −5: −5 >= 0 is false, so the check reports "not single-digit" — but −5 obviously is one. A slightly better version is if (n >= -9 && n <= 9), which works for all decimal integers. That's the shortest correct range check.
But the assignment asks for a loop-based approach, and there's a good reason to learn it. The loop version generalises immediately — if you can count digits, you can also answer "is this a two-digit number?", "is this a four-digit number?", "what's its digit count?" — while the range check has to be rewritten each time. So the loop is the more useful skill.
💡 In one line: a number is single-digit if and only if it has exactly one digit. Count the digits, then check whether the count is 1.
Counting Digits Without Losing the Original
The loop is nearly identical to the count-digits program from assignment 6, with one important consideration: if you need the original value of n after the loop, you have to preserve it. You could either save it in a temporary variable, or write the loop in a way that doesn't destroy n. In this case, the check is count == 1, so the original value isn't strictly needed — but it's a good habit to preserve it anyway in case the program grows.
The loop runs at least once because of the do-while. For input 0, the body executes, extracts digit 0 (a no-op as far as digit count is concerned), divides 0 by 10 → 0, count becomes 1, condition fails, exit. Count = 1 → single-digit. Correct, without any special case.
For input 42, the body runs twice: first iteration divides 42 → 4, count = 1; second iteration divides 4 → 0, count = 2. Then the loop exits and count = 2, so "not single-digit". Correct.
| Input | Digit count | Single-digit? | Notes |
|---|---|---|---|
| 7 | 1 | Yes | Ordinary single-digit |
| 0 | 1 | Yes | Zero has exactly one digit |
| −5 | 1 | Yes | Minus sign isn't a digit |
| 10 | 2 | No | Smallest two-digit |
| −99 | 2 | No | Largest negative two-digit |
| 100 | 3 | No | Three digits |
| 2147483647 | 10 | No | Largest int |
Complete C Program
Program: check for single-digit using a do-while loop
#include <stdio.h>
int main(void) {
int n;
printf("Enter an integer: ");
if (scanf("%d", &n) != 1) {
printf("Invalid input.\n");
return 1;
}
/* The minus sign isn't a digit — work with magnitude only */
if (n < 0) n = -n;
/* Preserve the original so we can display it in the result message */
int original = n;
/* do-while runs the body before checking the condition, so
even n = 0 contributes one iteration (and one digit) */
int count = 0;
do {
count++;
n /= 10;
} while (n != 0);
if (count == 1) {
printf("%d is a single-digit number.\n", original);
} else {
printf("%d is not a single-digit number (it has %d digits).\n", original, count);
}
return 0;
}
And here's the loop-free range-check version, for comparison. It's shorter, but it doesn't report the digit count, and it doesn't generalise:
Range-check version (no loop)
#include <stdio.h>
int main(void) {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
if (n >= -9 && n <= 9) {
printf("%d is a single-digit number.\n", n);
} else {
printf("%d is not a single-digit number.\n", n);
}
return 0;
}
The range check is faster (no loop), but it only answers "is it single-digit?" — a different question from "how many digits?" The loop version gives you the count directly, so you can ask "is it two-digit?" or "is it at most four-digit?" with a one-line change. For the assignment, use the loop version; for production code where you're sure you only need the single-digit check, the range check is fine.
Program Output
Enter an integer: 7 7 is a single-digit number.
A few more cases. Input 42 gives "42 is not a single-digit number (it has 2 digits)." Input 0 gives "0 is a single-digit number." Input −5 gives "5 is a single-digit number." (Note: the minus sign isn't shown because we took the absolute value and stored it in original — if you want to preserve the sign in the display, don't reassign n; save the sign in a separate variable first.) Input 100 gives "100 is not a single-digit number (it has 3 digits)." Input 2147483647 gives "2147483647 is not a single-digit number (it has 10 digits)."
Step-by-Step Explanation
Let's walk through 42, then 0, and then look at the small subtleties.
1. Input and normalisation
Negative numbers are converted to positives first, so −42 becomes 42. This is important because the digit-count loop should count digits, not the minus sign. If you skipped this step, −42 would still give the right digit count (2) — but only because −42 / 10 = −4, −4 / 10 = 0, both with two iterations. Negative division truncates toward zero in C, so the loop happens to work. But it's fragile — the behaviour depends on C's division rule — and normalising is clearer.
2. Preserving the original value
After normalising, original = n saves the current value. Then the loop modifies n in place. If you need to display the input in the result message (or use it later), you need this saved copy. Without it, the loop would leave n at 0, and the output would print "0 is a single-digit number" — confusing for the user who typed 42.
If you don't need to display the input, you can skip this. The loop's effect on n is fine as long as you only need count afterwards.
3. The do-while loop
For 42, the loop runs twice:
- Iteration 1:
count = 1,n = 42 / 10 = 4 - Iteration 2:
count = 2,n = 4 / 10 = 0 - Condition
n != 0is false → exit
For 0, the loop runs once:
- Iteration 1:
count = 1,n = 0 / 10 = 0 - Condition
n != 0is false → exit
Count = 1, so 0 is single-digit. Correct. If we'd used a while (n != 0) loop instead, the body would never have run for n = 0, and count would stay 0 — the wrong answer.
4. The single-digit test
After the loop, count holds the total digit count. The test if (count == 1) is the whole verification. This is more reliable than a range check because it captures the actual question — "how many digits?" — directly. A number is single-digit exactly when its digit count is 1; that's the definition, not an approximation.
5. Why the range check is tempting but limited
The range check if (n >= -9 && n <= 9) is tempting because it's one line. But it's more error-prone than it looks. Newcomers often write n >= 0 && n <= 9, forgetting negatives. Others write n <= 9 alone, forgetting that −100 is also n <= 9. The version shown above is correct, but it's easy to get wrong. And it doesn't extend to "is it two-digit?" — you'd need another range check with a different bound. The loop version has one correct form and generalises.
6. Why "digit count" is the right abstraction
There's a general lesson hiding in this program. The question "is it a single-digit number?" is really a specific instance of the question "how many digits does it have?" — and the loop answers the more general question first. This is often a good strategy: solve the general version, then specialise. Not only does it make the code more reusable, it also removes a class of edge-case bugs. The single-digit check falls out of the digit count as one line; getting the count wrong is harder to overlook than getting the range check wrong.
7. Extending the pattern
Once you have the loop, the single-digit check is one line. Two-digit is another line: count == 2. "Any number with at most 3 digits": count <= 3. "Number of digits greater than 5": count > 5. All variations on the same loop. The range-check version doesn't have this flexibility — you'd need n >= -999 && n <= 999 for "at most three digits", which is easy to get wrong.
🎯 Remember this: a number is single-digit if and only if it has exactly one digit. Count the digits with a do-while loop (which handles zero naturally), then check whether the count is 1. The loop generalises to any digit-count check; the range check doesn't.
Frequently Asked Questions
1. Why does the assignment ask for a do-while loop?
Because every integer has at least one digit, so the loop must run at least once. A while (n != 0) loop would fail for input 0 — the body wouldn't run, the count would stay 0, and the program would report "0 has 0 digits" (wrong). The do-while runs the body before checking the condition, so zero gets counted correctly. This is the same reason do-while appears in the largest-digit and product-of-digits programs.
2. Is 0 a single-digit number?
Yes. Zero is written with exactly one digit: 0. Any correct single-digit check must include zero, which is why the range check is n >= 0 && n <= 9 (for non-negatives) or, if handling negatives, n >= -9 && n <= 9. The loop version gets this right automatically because the do-while counts zero as one digit.
3. Why do you save the original value?
Because the loop destroys n (divides it down to 0). If you want to print the input in the result message (like "42 is not a single-digit number"), you need to keep a copy. In the code shown, int original = n; saves the normalised value before the loop runs. Without this, the final message would print "0 is not a single-digit number" — confusing because the user typed 42. If you don't need to display the input, you can skip the copy.
4. What if I use a range check instead of a loop?
It's shorter and faster. The correct range check is if (n >= -9 && n <= 9) — make sure both bounds are there, because n <= 9 alone would incorrectly classify -100 as single-digit. If you use it, add a comment explaining what you're doing, and be aware it only answers "is it single-digit?", not "how many digits?". For the assignment, use the loop; for production code, either works.
5. Can I extend this to check for two-digit numbers or three-digit numbers?
Yes — the loop-based approach makes this easy. Just change the test. "Is two-digit?" becomes if (count == 2). "Is at most three-digit?" becomes if (count <= 3). "Is exactly four-digit?" becomes if (count == 4). Each one is a single-line change, no new logic required. The range-check version would need a new range for each case, which is more error-prone.
6. Is the digit count the most efficient way to check for single-digit?
No — the fastest way is the range check, which is a single comparison. But "most efficient" isn't always "best". The digit-count approach is more general (extends to any digit count), more robust (no off-by-one errors in the range), and easier to read (the question "how many digits?" is what you're really asking). For a single-check, the difference is microseconds; for a loop over a million numbers, it might matter. Choose based on what you need.
💡 Tip: After this program, try modifying it to answer "is this a two-digit number?" — you just change count == 1 to count == 2. Then try "how many digits does this number have?" — just print the count. You'll see that every variation reuses the same loop. That's a sign you've built the right abstraction. Once you have a general digit counter, every "is it N-digit?" question becomes trivial.