Simple Reverse a Number program !

Are you a budding programmer diving into the world of Java? If so, you’ve come to the right place! In this article, we’ll explore how to write a Java program to find the reverse of a number. Whether you’re a seasoned coder looking for a quick refresher or a beginner taking the first steps in programming, this guide will walk you through the process step by step.

Introduction to Reversing a Number

Reversing a number involves rearranging its digits in the opposite order. For example, reversing the number 12345 would result in 54321. This seemingly simple task holds significant importance in programming and can be a building block for more complex algorithms.

Basic Concepts of Java Programming

Before we dive into the actual code, let’s briefly cover some fundamental concepts of Java programming that you’ll need to understand.

Java is an object-oriented programming language known for its platform independence, robustness, and versatility. It uses a syntax similar to C++, making it relatively easy to learn for those familiar with other programming language

Using Modulus Operator

One way to reverse a number is by using the modulus operator (%) to extract the digits one by one. Here’s a high-level overview of the process:

  1. Initialize variables to store the original and reversed numbers.
  2. Iterate through the digits of the original number using a loop.
  3. Extract the last digit using the modulus operator and add it to the reversed number.
  4. Remove the last digit from the original number using integer division.
  5. Repeat steps 3 and 4 until all digits are processed.

Writing the Java Program

Reverse a Number Using While Loop –

public class reversenum {

public static void main(String agrs[])
int n = 12345, reverse=0;
while (n != 0)
int remainder = n%10;
reverse = reverse * 10+remainder;
System.out.println("reverse number is :" +reverse);
OUTPUT : reverse number is : 54321

Compiling and Running the Program

To compile and run the program, follow these steps:

  1. Save the Java code in a file named
  2. Open a terminal or command prompt.
  3. Navigate to the directory containing the file.
  4. Compile the program by entering the command: javac
  5. Run the compiled program with the command: java reversenum


Congratulations! You’ve successfully learned how to write a Java program to reverse a number using different techniques.