Reverse a Number: flowchart, algorithm and program

This program reads an integer n and prints its digits in reverse order, so 1234 becomes 4321. It peels off the last digit with % 10, adds it to the end of rev, and then drops that digit from n with / 10.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Set rev = 0, digit.
  3. Step 3: Read n.
  4. Step 4: If n != 0, go to step 5; otherwise go to step 8.
  5. Step 5: Set digit = n % 10.
  6. Step 6: Set rev = rev * 10 + digit.
  7. Step 7: Set n = n / 10. Go to step 4.
  8. Step 8: Print rev.
  9. Step 9: Stop.

Pseudocode

BEGIN
    rev ← 0, digit
    READ n
    WHILE n != 0 DO
        digit ← n % 10
        rev ← rev * 10 + digit
        n ← n / 10
    END WHILE
    PRINT rev
END

Program in C, Python and Java

Reverse a Number program in C

#include <stdio.h>

int main() {
    int n, rev = 0, digit;
    printf("Enter n: ");
    scanf("%d", &n);
    while (n != 0) {
        digit = n % 10;
        rev = rev * 10 + digit;
        n = n / 10;
    }
    printf("Reversed number: %d\n", rev);
    return 0;
}

Reverse a Number program in Python

n = int(input("Enter n: "))
rev = 0
while n != 0:
    digit = n % 10
    rev = rev * 10 + digit
    n = n // 10
print("Reversed number:", rev)

Reverse a Number program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n, rev = 0, digit;
        System.out.print("Enter n: ");
        n = sc.nextInt();
        while (n != 0) {
            digit = n % 10;
            rev = rev * 10 + digit;
            n = n / 10;
        }
        System.out.println("Reversed number: " + rev);
    }
}

Sample input: n = 1234
Output: Reversed number: 4321

How it works

  • rev starts at 0. It will collect the reversed digits one by one.
  • Inside the while (n != 0) loop, digit = n % 10 takes the last digit of n. For 1234 that is 4.
  • rev = rev * 10 + digit shifts the digits already in rev one place left and puts the new digit at the end. So rev goes 0 → 4 → 43 → 432 → 4321.
  • n = n / 10 removes the last digit using integer division (1234 → 123 → 12 → 1 → 0).
  • When n becomes 0 the loop stops, and rev holds the reversed number, which is printed.

Dry run (trace table)

StepLineBoxrevndigitOutput
13Start
24rev = 0, digit0
36Read n01234
47n != 0?01234
58digit = n % 10012344
69rev = rev * 10 + digit412344
710n = n / 1041234
87n != 0?41234
98digit = n % 1041233
109rev = rev * 10 + digit431233
1110n = n / 1043123
127n != 0?43123
138digit = n % 1043122
149rev = rev * 10 + digit432122
1510n = n / 1043212
167n != 0?43212
178digit = n % 1043211
189rev = rev * 10 + digit432111
1910n = n / 10432101
207n != 0?432101

Time and space complexity

Time: O(log n) · Space: O(1). The loop runs once per decimal digit of n (about log10 n times) using only a few variables.

Common mistakes

  • Forgetting to set rev = 0 before the loop, so it starts with a garbage value and the answer is wrong.
  • Writing rev = rev + digit instead of rev = rev * 10 + digit, which just adds up the digits instead of reversing them.
  • Forgetting n = n / 10, which makes the loop run forever because n never reaches 0.

Viva questions

Q1. Why is rev initialised to 0 before the loop?

rev builds the answer digit by digit using rev * 10 + digit. Starting at 0 means the first digit is added to nothing, and an uninitialised rev would hold garbage.

Q2. What does n % 10 do on line 8, and what does n / 10 do on line 10?

n % 10 gives the last digit of n. n / 10 is integer division, so it drops the last digit. Together they peel off digits from right to left.

Q3. Trace the program for n = 123. What are n and rev after each iteration?

Start n=123, rev=0. Iteration 1: digit=3, rev=3, n=12. Iteration 2: digit=2, rev=32, n=1. Iteration 3: digit=1, rev=321, n=0. The loop stops and prints 321.

Q4. Why is a while loop used instead of a for loop? Could you use a for loop?

We don't know in advance how many digits the number has, so we loop until n becomes 0. A for loop could also work, but while is the natural fit for a condition-driven loop.

Q5. What happens if the user enters 0? What if the number ends in zeros, like 1200?

For 0 the loop never runs and rev stays 0, which is the correct output. For 1200 the output is 21, because the leading zeros of the reversed number vanish in an int.

Q6. What happens with a negative input such as -45?

In C, % and / truncate toward zero, so the digits come out as -5 and -4. rev becomes -54, so the program happens to work and prints the reversed number with its sign.

Q7. What can go wrong with a large input like 1999999999?

The reversed value 9999999991 is larger than INT_MAX, so rev overflows, which is undefined behaviour for signed int. Using long long for rev, or checking before multiplying, would fix this.

Q8. What is the time complexity of this program?

O(d), where d is the number of digits, which is O(log10 n). Each iteration removes one digit and does constant work. Extra space is O(1).

Q9. What would you change to check whether a number is a palindrome?

Save the original n in another variable before the loop, because the loop destroys n. After the loop, compare the saved value with rev. If they are equal, the number is a palindrome.

Frequently asked questions

Why do we use % 10 and / 10 to reverse a number?

n % 10 gives the last digit of n, and n / 10 (integer division) removes that last digit. Repeating both until n is 0 visits every digit from right to left.

What happens to trailing zeros, like reversing 1200?

The result is 21, not 0021, because an int does not keep leading zeros. The digits 0, 0, 2, 1 are added in order, and rev stays 0 until the first non-zero digit arrives.

Does this program work for negative numbers?

Yes, in C. The % operator keeps the sign of n, so digit is negative and rev ends up negative too (-123 gives -321). The loop still ends when n reaches 0.

More programs

← All programs

Try AI Flowchart free →