Progressive Number Formation Pattern in C

What You’ll Learn
How to form a number step by step while extracting digits from right to left. Each line prints the intermediate value after appending the next digit.
⭐ Pattern Output
For num = 86523, the pattern looks like this:
3
32
325
3256
32568Complete C Program
Extract a digit with num % 10, append it using reverse = reverse * 10 + digit, print, and then remove the digit with num / 10.
#include <stdio.h>
int main() {
int num = 86523;
int reverse = 0;
while (num != 0) {
reverse = reverse * 10;
reverse = reverse + (num % 10);
printf("%d\n", reverse);
num = num / 10;
}
return 0;
}🧠 How It Works
Extract last digit
num % 10 gives the rightmost digit (e.g., 86523 % 10 = 3).
Append to reverse
Multiply by 10 to shift, then add the digit: reverse = reverse * 10 + digit.
Remove processed digit
num = num / 10 drops the last digit so we can process the next one.
Progressive formation
One loop iteration per digit, so runtime is O(log n).
Variation — User Input Version
Read the number from the user with scanf().
#include <stdio.h>
int main() {
int num;
int reverse = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
reverse = reverse * 10;
reverse = reverse + (num % 10);
printf("%d\n", reverse);
num = num / 10;
}
return 0;
}💡 Tips for Enhancement
Try These
- Print the final formed number after the loop ends
- Count processed digits and print the count
- Compare the final value with the original for palindrome checks
Avoid
- Assuming this prints something for
num=0without handling it separately - Ignoring possible overflow for very large inputs (use
long longif needed)
Key Takeaways
num % 10 extracts the last digit.
reverse = reverse * 10 + digit appends a digit.
The loop runs once per digit: O(log n).
Printing each step helps visualize number formation.
❓ Frequently Asked Questions
Explore More C Number Patterns!
Try more digit-manipulation patterns and loop-based variations.
num % 10 extracts the last digit, while num / 10 removes the last digit (integer division).
12 people found this page helpful
