C Number Pattern (Progressive Reverse)

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

What Is This Pattern?

A growing reverse-number pattern builds the reverse of an integer one digit at a time — printing the partial reverse after each append — so digits appear from right to left of the original.

Remember
Rule: digit = num % 10
      reverse = reverse * 10 + digit
      print reverse; num = num / 10

3
32
325
3256
32568     ← start = 86523

Follows the remove-last-digit pattern in Program 60; next is the spiral matrix in Program 62.

How to Solve It

Peel digits from the right with %, append them into reverse, print after each append, then shrink num with / 10.

MethodIdeaBest for
while + modulo buildreverse = reverse * 10 + (num % 10), then printLearning, interviews, digit drills
vs Program 6060 shrinks num; 61 grows a separate reverseComparing digit techniques

Pseudocode

Pseudocode
reverse = 0
while num is not 0:
    reverse = reverse * 10 + (num % 10)
    print reverse
    num = num / 10

Cheat sheet

GoalPattern
Loop conditionwhile (num != 0)
Last digitnum % 10
Append digitreverse = reverse * 10 + (num % 10);
Print partialprintf("%d\n", reverse);
Next digitnum /= 10;

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", reverse)Stays on the same lineRare here — values would glue together
printf("%d\n", reverse)Prints the value and ends the lineEach partial reverse (WriteLine style)

Like Program 60, each iteration needs its own newline — so %d\n combines Write + WriteLine in one call.

Live Preview

Change the starting number and the growing reverse 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 result num = 86523 · 5 lines
3
32
325
3256
32568

Worked Walkthrough — num = 86523

Trace how each rightmost digit is appended into reverse and printed.

Stepnum % 10reverse after append
133
2232
35325
463256
5832568

Final line 32568 is the full reverse of 86523. Five digits → five lines → O(d).

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 — append each rightmost digit into reverse and print after every step.

C
#include <stdio.h>

int main(void)
{
    int num = 86523;
    int reverse = 0;

    while (num != 0)
    {
        reverse = reverse * 10 + (num % 10);
        printf("%d\n", reverse);
        num = num / 10;
    }

    return 0;
}

How It Works

1. Peel. num % 10 reads the rightmost digit (3 from 86523).

2. Append. reverse * 10 + digit shifts left and adds the new digit on the right.

3. Shrink. num / 10 drops that digit so the next iteration uses the next one.

Example 2 — User Input Number

Read with long long, take absolute value, then run the same append-and-print loop.

C
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    long long num;
    long long reverse = 0;

    printf("Enter a number: ");
    if (scanf("%lld", &num) != 1)
    {
        printf("Please enter a valid integer.\n");
        return 1;
    }

    num = llabs(num);

    while (num != 0)
    {
        reverse = reverse * 10 + (num % 10);
        printf("%lld\n", reverse);
        num /= 10;
    }

    return 0;
}

How It Works

1. Wider type. long long holds larger starts before reverse overflows.

2. Absolutize. llabs(num) keeps digit math positive.

3. Trailing zero demo. 120 peels 0 first → lines 0, 2, 21.

Example 3 — Compact num = 123

Three lines only — easy to dry-run on paper before larger demos.

C
#include <stdio.h>

int main(void)
{
    int num = 123;
    int reverse = 0;

    while (num != 0)
    {
        reverse = reverse * 10 + (num % 10);
        printf("%d\n", reverse);
        num /= 10;
    }

    return 0;
}

How It Works

1. Three digits. Peel 3, then 2, then 1 → 3, 32, 321.

2. Trace on paper. Confirm you print after appending — that is what grows each line.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for 86523 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

print before append

Stale reverse values

If you print before updating reverse, the first line is 0 (or empty of the new digit). Append, then print.

forget * 10

Digits overwrite

Without reverse * 10, you only keep the latest digit — never a growing number.

trailing 0

Leading zero line

120 prints 0 first because the rightmost digit is zero — expected, not a bug.

num = 0

Empty output

while (num != 0) skips entirely when the start is already 0.

int overflow

Large reverses wrap

Very long inputs can overflow int. Prefer long long for interactive programs (Example 2).

scanf

Check the return value

Validate scanf before the loop — a failed read leaves num uninitialized.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(d)O(1)
Compact num = 123 (Example 3)O(d)O(1)

Each iteration handles one digit, so the loop runs d times for a d-digit start — linear in digit count, constant extra memory.

Key Takeaways

  • Append then print: reverse = reverse * 10 + (num % 10), then printf.
  • Modulo peels right: % 10 reads the next digit; / 10 advances.
  • vs Program 60: 60 shrinks the original; 61 grows a separate reverse.
  • Complexity: O(d) time for d digits; O(1) extra space.

One line: reverse = reverse * 10 + num % 10, print, then num /= 10.

Frequently Asked Questions

It prints a growing reverse-number pattern like 3, 32, 325, 3256, 32568 when starting from 86523.
It takes the last digit using num % 10 and appends it with reverse = reverse * 10 + digit, then prints reverse.
num = num / 10 removes the last digit so the loop can move to the next digit from the right.
Program 60 prints the shrinking original number. Program 61 builds and prints a growing partial reverse using modulo and multiplication.
Multiplying shifts existing digits left — reverse * 10 + digit appends the new digit on the right.
If the source ends in 0, that digit is extracted first — e.g. 120 gives 0, then 2, then 21.
Apply abs (or llabs for long long) before the loop — see Example 2.
O(d) where d is the number of digits — the loop runs once per digit.

Did you know?

Each iteration takes the last digit with num % 10, appends it to reverse via reverse = reverse * 10 + digit, prints the partial reverse, then shrinks num — runtime is O(d) for d digits.

Next: Perfect Square Spiral

Continue with a 2D spiral that fills an n×n grid with consecutive numbers.

Program 62 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