A remove-last-digit pattern prints an integer, then repeatedly strips the rightmost digit with integer division by 10 until the value becomes zero.
Remember
Rule: while num != 0 → print num → num = num / 10
86523
8652
865
86
8 ← start = 86523
Follows the hollow square border in Program 59; next is the growing reverse pattern in Program 61.
Approach
How to Solve It
Use a while loop: print the current value, then divide by 10. Stop when num reaches 0.
Method
Idea
Best for
while + divide by 10
Print, then num /= 10 until zero
Learning, interviews, digit drills
abs for negatives
num = abs(num) before the loop
User input that may be negative
Pseudocode
Pseudocode
while num is not 0:
print num
num = num / 10
Cheat sheet
Goal
Pattern
Loop condition
while (num != 0)
Print current
printf("%d\n", num);
Drop last digit
num = num / 10; or num /= 10;
Handle negatives
num = abs(num); (needs <stdlib.h>)
Line count
Equals the digit count of the starting number
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", num)
Stays on the same line
Rare here — digits would glue together
printf("%d\n", num)
Prints the value and ends the line
Each step of the pattern (WriteLine style)
Unlike nested triangle patterns, each iteration needs its own newline — so %d\n combines Write + WriteLine in one call.
Try it
Live Preview
Change the starting number and the digit-removal pattern updates instantly — absolute value, up to 9 digits.
Whole numbers from 1 to 999999999 (negatives use absolute value). Tap a chip or type a value.
Live resultnum = 86523 · 5 lines
86523
8652
865
86
8
Trace
Worked Walkthrough — num = 86523
Trace each print-then-divide step until the loop stops.
Step
Prints
After / 10
1
86523
8652
2
8652
865
3
865
86
4
86
8
5
8
0 (loop ends)
Five digits → five lines → O(d) time. Integer division truncates toward zero, so no floating-point is involved.
Code
C Programs
Three complete programs: fixed num = 86523, scanf input, and a compact num = 123 demo. Use View Output to reveal sample results.
Example 1 — Fixed num = 86523
Hard-coded start — print, then divide by 10 until zero.
C
#include <stdio.h>
int main(void)
{
int num = 86523;
while (num != 0)
{
printf("%d\n", num);
num = num / 10;
}
return 0;
}
Output
86523
8652
865
86
8
How It Works
1. Print first. Show the current num before changing it.
2. Divide by 10. Integer division drops the last digit — 86523 / 10 is 8652.
3. Stop at zero. When num becomes 0, while (num != 0) ends.
Example 2 — User Input Number
Read num with scanf, take absolute value, then run the same loop.
C
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num;
printf("Enter a number: ");
if (scanf("%d", &num) != 1)
{
printf("Please enter a valid integer.\n");
return 1;
}
num = abs(num);
while (num != 0)
{
printf("%d\n", num);
num /= 10;
}
return 0;
}
Output (when user enters 120)
Enter a number: 120
120
12
1
How It Works
1. Prompt and validate. Check scanf before using num.