Number Pattern with Right Digit Removal in C

What You’ll Learn
How to print a decreasing pattern by repeatedly removing the rightmost digit from a number using integer division (num / 10).
⭐ Pattern Output
For num = 86523, the pattern looks like this:
86523
8652
865
86
8Complete C Program
Print the number, then do num = num / 10 until it becomes 0.
#include <stdio.h>
int main() {
int num = 86523;
while (num != 0) {
printf("%d\n", num);
num = num / 10;
}
return 0;
}🧠 How It Works
Start with a number
num holds the current value to print.
Print and divide by 10
After printing, num = num / 10 removes one digit from the right.
Stop at zero
When the number becomes 0, there are no digits left to remove.
Right-digit removal
One loop iteration per digit, so runtime grows with the number of digits.
Variation — User Input Version
Read the starting number from the user using scanf().
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
printf("%d\n", num);
num = num / 10;
}
return 0;
}💡 Tips for Enhancement
Try These
- Count digits removed and print the count per line
- Store values and print them bottom-up for a reversed pattern
- Extract digits using
num % 10before dividing
Avoid
- Using negative numbers without handling sign
- Assuming this prints something for
num = 0(it won’t)
Key Takeaways
Integer division by 10 removes the last digit.
The loop runs once per digit: O(log n).
Stopping at num != 0 ends after the last single digit is printed.
This technique is useful for digit-manipulation problems.
❓ Frequently Asked Questions
Explore More C Number Patterns!
Try more digit-manipulation patterns and loop-based variations.
Dividing by 10 removes the last digit, while num % 10 extracts the last digit.
12 people found this page helpful
