Reverse a given number using Java

Reverse of a number in java :


In this article, we will create a java program to reverse a number. Here we will ask the user to enter a number and then by the help of the java code we will reverse the number and display it on the screen.


Reversing of a number generally means printing the number in reverse order. 


On most of the sites, the reverse of a number is done in wrong way because if you put 90, 100, 1300 and many other inputs which have ‘ 0 ’ in last digits of the number then it will neglect 0 in their output. So, below mentioned code is made after considering all the cases.


For better understanding let’s consider some examples :

  1. 1900 : Reverse is 0091 
  2.  95724 : Reverse is 42759
Reverse a given number using java

Working :

Step 1 : Ask the user to enter a number.

Step 2 : Declare a string variable to store reversed number.

Step 3 : Use a loop to perform continuous iterations till the number entered by user becomes 0.

Step 4 : Pick every last digit of the number at each iteration and store it in a variable pick_last.

Step 5 : Use a statement to reverse the number.

Step 6 : Change the value of number by removing the last digit which is stored in the pick_last variable.

Step 7 : Repeat the Step 4 to 6 till the condition of step 3 gets false.

Step 8 : Display the reversed string.

Code in Java :

//Java program to print reverse of a number
import java.util.Scanner;
public class reverse_of_number
public static void main(String[] args)
//scanner class declaration
Scanner sc = new Scanner(;
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
System.out.print("Reverse of "+number+" is ");
int reverse = 0;
String s = "";
while(number != 0)
int pick_last = number % 10;
//use function to convert pick_last from integer to string
s = s + Integer.toString(pick_last);
number = number / 10;
//display the reversed number
//closing scanner class(not compulsory, but good practice)

Output :

Enter a number : 123

Reverse of 123 is 321

Enter a number : 9000

Reverse of 9000 is 0009

One comment on “Reverse a given number using Java”

  • Aditi

    import java.util.*;
    public class ReverseNumber {
    public static void main(String[] args)
    Scanner sc =new Scanner(;
    int number=sc.nextInt();
    int reverse =0;
    int lastDigit=number%10;
    reverse =reverse*10+lastDigit;
    number =number/10;
    System.out.print(“Sum of Digit is:”+reverse);