Program to reverse a number in java with explanation

In this tutorial we are going to learn how to write a program to reverse the digits of a given number in Java programming language.

So Lets  know what we are going to achieve in this program before moving on writing program.


Suppose if someone given input to the program like 456789. After performing reverse operation program our Java program will return output 987654.


We are following logic here like first we will take last digit of  the input number and store it  in a variable. Now we will divide it by 10 so that we can find middle number if exist as last number and append it in previous stored number.

By following this approach we can find reverse of number.

Java program to reverse a number

import java.util.*;  
class Main {
    public static void main(String ...a){
    int reverse=0;
    Scanner sc = new Scanner(;
    System.out.println("Please give a number: ");
    int i = sc.nextInt(); 
		reverse = reverse*10 + i%10;        
	System.out.println("Number after reverse : "+reverse);    


java program to reverse a number

Leave a Comment