C++ Number Pattern (Remove Last Digit)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A remove-last-digit number pattern prints an integer, then repeatedly strips the rightmost digit with integer division by 10 until the value reaches zero.

Remember
Rule: print num, then num = num / 10, until num == 0

86523
8652
865
86
8     ← start = 86523

Unlike Program 59 (nested grid loops), this pattern uses one while loop on a single number — a foundation for digit counting and reversals.

How to Solve It

Loop while the number is not zero: print the current value, then divide by 10 to drop the last digit.

MethodIdeaBest for
While + dividePrint, then num /= 10Learning, interviews, demos
Compact traceSame logic with num = 123Quick dry-runs on paper

Pseudocode

Pseudocode
while num is not 0:
    print num
    num = num / 10

Cheat sheet

GoalPattern
Loop until emptywhile (num != 0)
Print current valuecout << num << "\n";
Drop last digitnum = num / 10; or num /= 10;
Handle negativesif (num < 0) num = -num;
Lines printedEquals digit count of the starting number

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << numStays on the same lineThe current value
cout << "\n" / cout << num << "\n"Ends the lineEach shrinking step

Each step prints one complete number and ends the line (WriteLine-style).

Live Preview

Change the starting number and the shrinking pattern updates instantly.

Enter a positive integer up to 9 digits. Tap a chip or type a value — the preview redraws as you go.

Live result 86523 · 5 lines
86523
8652
865
86
8

Worked Walkthrough

Trace num = 86523 — print first, then divide by 10.

StepPrintsAfter num /= 10
1865238652
28652865
386586
4868
580 (loop ends)

Five lines for a five-digit start — each iteration removes exactly one digit.

C++ Programs

Three complete programs: fixed 86523, cin input, and a compact 123 demo. Use View Output to reveal sample results.

Example 1 — Fixed num = 86523

Hard-coded start — print, divide by 10, repeat until zero.

C++
#include <iostream>
using namespace std;

int main()
{
    int num = 86523;

    while (num != 0)
    {
        cout << num << "\n";
        num = num / 10;
    }

    return 0;
}

How It Works

1. Guard the loop. while (num != 0) keeps going until every digit is stripped.

2. Print first. Output the current value before changing it, so every prefix appears.

3. Drop the last digit. Integer num / 10 discards the remainder — no floating point needed.

Example 2 — User Input Number

Read the starting value at runtime and normalize negatives before the loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int num;

    cout << "Enter a number: ";
    cin >> num;

    if (num < 0)
        num = -num;

    while (num != 0)
    {
        cout << num << "\n";
        num /= 10;
    }

    return 0;
}

How It Works

1. Same loop core. Only the source of num changes from a literal to cin.

2. Negatives flip sign. Digit removal works on the magnitude, so -9876 shrinks like 9876.

3. Validate in real apps. Prefer checking cin failure before the loop (tip below).

Safer input tip
if (!(cin >> num))
{
    cout << "Please enter a valid integer.\n";
    return 1;
}

Example 3 — Compact num = 123

Three iterations — ideal for a paper dry-run before larger demos.

C++
#include <iostream>
using namespace std;

int main()
{
    int num = 123;

    while (num != 0)
    {
        cout << num << "\n";
        num /= 10;
    }

    return 0;
}

How It Works

1. Only three steps. Easy to dry-run print → divide on paper.

2. Same formula. Nothing changes except the starting value — proving the pattern scales.

3. Ends at 1. After printing 1, 1 / 10 becomes 0 and the loop stops.

Edge Cases & Pitfalls

Check these before calling the solution done.

Infinite loop

Always update num inside the loop

Forgetting num /= 10 leaves the condition true forever.

Zero start

num = 0 prints nothing

while (num != 0) never enters. Decide whether to print a single 0 for that case.

Trailing zeros

Trailing zeros disappear on the next line

120 becomes 12, then 1 — expected with integer division.

Bad cin

Validate input

Check cin >> num for failure before running the loop.

Time and Space Complexity

ProgramTimeExtra space
While + divide (Examples 1–3)O(d)O(1)

Each iteration removes one digit, so a d-digit number runs in linear time in d. Extra memory is only the loop variable.

Key Takeaways

  • Rule: print num, then num /= 10, until num == 0.
  • Integer division: / 10 on int drops the last digit cleanly.
  • Print then shrink: output before dividing so every prefix appears.
  • Complexity: O(d) time, O(1) extra space.

One line: print the number, divide by 10, repeat until nothing remains.

Frequently Asked Questions

It prints the starting number on each line while removing the last digit each step. For 86523: 86523, 8652, 865, 86, 8.
Integer division by 10 discards the remainder: 86523/10 becomes 8652, then 865, then 86, then 8.
The number of steps equals the digit count — unknown until you read the input. while (num != 0) keeps going until all digits are stripped.
Program 59 uses nested loops on a grid. Program 60 uses one while loop and integer division on a single number.
Program 60 shrinks the original number by dividing by 10. Program 61 builds a growing reverse number using modulo and division.
A trailing 0 is removed on the next step — 120 becomes 12, then 1.
You can print the number and the newline together with cout << num << "\n". That is the WriteLine-style end of each step.
O(d) where d is the number of digits — each iteration removes exactly one digit.

Did you know?

Each iteration prints the current number, then num = num / 10 drops the last digit using integer division — runtime is O(d) for d digits.

Next: Growing Reverse Number Pattern

Continue with the next pattern in the C++ number-pattern series.

Program 61 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful