Reverse a Number in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

/* C - Reverse a Number (short & working) */
#include <stdio.h>
int main() {
    int n, rev = 0;
    scanf("%d", &n);
    while (n != 0) {
        rev = rev * 10 + n % 10;
        n /= 10;
    }
    printf("%d\n", rev);
    return 0;
}

C Output

Input:
12345

Output:
54321



C++ Program

// C++ - Reverse a Number (short & working)
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, rev = 0; 
    cin >> n;
    while (n) {
        rev = rev * 10 + n % 10;
        n /= 10;
    }
    cout << rev << '\n';
}

C++ Output

Input:
9870

Output:
789



JAVA Program

// Java - Reverse a Number (short & working)
import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int n = s.nextInt(), rev = 0;
    while (n != 0) {
      rev = rev * 10 + n % 10;
      n /= 10;
    }
    System.out.println(rev);
  }
}

JAVA Output

Input:
4051

Output:
1504



Python Program

# Python - Reverse a Number (short & working)
n = int(input())
rev = 0
while n:
    rev = rev * 10 + n % 10
    n //= 10
print(rev)

Python Output

Input:
1200

Output:
21



In-Depth Learning – Entire Concept in Paragraphs
Example
When you enter 12345 in the C version, the algorithm takes the last number 5 (n % 10) and adds it to rev. rev is initially 0, so rev = 0 * 10 + 5 = 5. n then becomes 1234, and the loop continues until n is 0. The output is 54321.

Real-Life Analogy
Imagine writing a phone number on sticky notes, one digit per note, and then flipping the stack upside down. The top digit becomes the last, and the last digit becomes the first — that’s essentially what we’re doing with arithmetic operations here.

Learning Insights
This exercise solidifies loop control, modulus operator application, integer division, and digit extraction. You realize that n % 10 extracts the last digit and n / 10 (or n // 10 in Python) chops it off. By multiplying rev by 10 first and then adding the next digit, you "push" its digits to the left, creating room for the new digit on the right.

Why It Matters
It is not merely a math trick — it is a part of actual algorithms such as palindrome number verification, encryption procedures, digit-based calculations, and digital root calculations. Comprehension of digit extraction also branches out into more complicated issues like parsing integers, converting bases, and numerical transformations.

Interview Use Case
Interviewers tend to use "reverse a number" with conditions — reverse only if the number is positive, skip leading zeros, or verify whether the reversed number is equal to the original. They may further extend it to reverse just half of a number, operate with strings rather than integers, or reverse in alternate numeral bases.

Extensions and Optimization
For very big numbers, you could use strings rather than integers to prevent overflow in fixed-width languages. When reversing for palindrome testing, you can cut off in the middle. The same approach for extracting the digit works when reversing binary or hexadecimal forms.

How It Looks in Real Projects
In digital processing, reversal of digit sequences can be applied in checksum computations, some encoding processes, or game/puzzle algorithms. In embedded systems, digit reversal can be applied in display formatting when transmitting numbers to 7-segment displays from the least significant digit towards the most significant. 

Common Pitfalls
Inexperienced programmers often overlook handling such cases as trailing zeros (e.g., 1200 → 21), which indeed is correct for reversing an integer but can be surprising. The other very common mistake is floating-point division over integer division, introducing unwanted decimal points.

SEO-Optimized Paragraph
Number reversal in C, C++, Java, or Python is an easy programming problem that reinforces fundamentals of loops, modulus operation, and integer division. By pulling out numbers and reconstituting them in reverse format, you learn crucial logic employed in palindrome verification, digital calculations, and algorithm development. Regardless of whether you're gearing up for coding interviews, rehearsing number manipulation, or developing math-related applications, mastering reversing numbers fortifies your programming fundamentals as well as your capability to manage digit-based logic in practical coding problems.