11+ Java Programs to Reverse a Number

This post contains a total of 11+ Hand-Picked Java Programs to Reverse a Number with Source Codes. All the programs are made using Java Programming Language.

You can use the source code of these programs for educational purpose with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By World Champion

Made by World Champion. Simple Java program to reverse a input number. ( Source )

Reverse of a number 1234
Reverse of the entered number 1234 is 4321

import java.util.Scanner;

public class Program
{
    public static void main(String[] args) {
        
        
        Scanner s=new Scanner(System.in);
          
     //Enter a number
        System.out.println("Reverse of a number ");
        
        int n=s.nextInt();
        int num,digit,reverse=0;
        
        
        num=n;
        
          while(n>0){
              
              digit=n%10;
              
              reverse=10*reverse+digit;
              
              n/=10;
              
          }
          
          System.out.println("Reverse of the entered number "+num+" is "+reverse);
        
    }
}

2. By Nilesh

Made by Nilesh. Java program to reverse a integer. ( Source )

Your input is : 2345
After reversing :5432
import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int a = sc.nextInt();
        System.out.println("Your input is : " + a);
        int b,d=0;
        while(a>0){
            b=a%10;
            a=a/10;
            d=(d*10)+b;
        }
        System.out.println("After reversing :" +d);
    }
}

3. By ADITYA TIWARI

Made by ADITYA TIWARI. ( Source )

Enter A Number To Reverse 3456

6543
import java.io.*;
class Reverse
{
    public static void main(String[] args)throws Exception
    {
      
        InputStreamReader a=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(a);
          int n,r=0;
        System.out.println("Enter A Number To Reverse");
        n=Integer.parseInt(br.readLine());
        while(n!=0)
        {
            r=r*10;
            r=r+n%10;
            n=n/10;
        }
        System.out.println(+r);
    }
}

4. By Muhd Khairul Amirin

Made by Muhd Khairul Amirin. ( Source )

Enter a number: 5674
After reverse: 4765
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Reverse
{
    public static void main(String[] args) throws java.io.IOException
    {
 //enter a number
 
 try{
 int rev=0;

 int a=Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
 
 int b=a;
 
 while (a!=0){
     rev=rev*10;
     rev=rev+a%10;
     a=a/10;
     }
 System.out.println("Enter a number: "+b+"\nAfter reverse: "+rev+"\n");}
 
 catch(Exception e){
     System.out.print("Enter a number");
 }
 
    
 }
   
    }

5. By Shahid Iqbal

Made by Shahid Iqbal. Program to reverse a number using for loop. ( Source )

Enter You Number Buddy
4643
Reverse of the number: 4643 is = 3464
import java.util.*;
public class Program
{
    public static void main(String[] args) 
    {
        int rem,num,rev=0,myinput;
    
      Scanner input = new Scanner(System.in);
      
     System.out.println("Enter You Number Buddy");
      num = input.nextInt();
      myinput=num;
      
     for(;num!=0;)
     {
         //this will gives us reminder
         rem = num%10;
        
        //this will add the num nd after *
         rev = rev*10+rem;
         
         //this will give us quotient
         num = num/10;
     }      
  System.out.println("Reverse of the number: "+myinput+" is = "+rev);

    }
}

6. By Miljan Stojanovic

Made by Miljan Stojanovic. ( Source )

Your number is:           4574
Reverse number is:        4754
import java.util.Scanner;

public class ReverseGivenNumber
{
    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
        System.out.printf("%-26s%d\n", "Your number is: ", number );
        System.out.printf("%-25s ", "Reverse number is: ");
        
        while(number>0)
        {
            int x = number % 10;
            number /= 10;
            System.out.print(x);
        }
        
        
    }
}

7. By Ziv

Made by Ziv. This program gets input of Integer number from the user, returns its reversed order of digits their number. ( Source )

The No. you wrote: 1525
The reversed No. is: 5251
No. of digits: 4
import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
      Scanner usPick = new Scanner(System.in);
   int pick; //input from the user
   int count = 1, down = count; //counters
 try {  
   pick = usPick.nextInt(); //user pick 
   System.out.println("The No. you wrote: " + pick);  
   double m = pick; 
   System.out.print("The reversed No. is: ");
   while (count >= down) {  
    if (Math.abs(m) >= 10)  { //if the number contains more than one digit
      int digit = (int)(m%10); //last digit equals to the remainder of 10
      m/= 10; //devide by 10 to get the next digit each time
       if (pick<0 && count>1) { //if the number is negative
           digit*=-1;
       }
      down--;  //counts down
      count++; //counts the digits    
    System.out.print(digit);
      } else { //for one digit number
           if (pick<0) { //if the number is negative
            m*=-1;
       }
     System.out.println((int)m); //last digit
      down = count + 1; //Stop condition
      }
   }
   System.out.println("No. of digits: " +count);
  }
 catch (Exception e) {
       //input is not valid  
       System.out.println("Integer only!");     
      }
    }
}

8. By Looper๐Ÿ‡ฎ๐Ÿ‡ณ

Made by Looper๐Ÿ‡ฎ๐Ÿ‡ณ. Enter the number you want to reverse in int n=85952. ( Source )

Reverse Number is:25958
Number of digits is:5
public class Program
{
    public static void main(String[] args) {
        int n=85952,revNum=0,r,count=0;

        while(n>0)
        {
            r=n%10;
            revNum=(revNum*10)+r;
            n=n/10;
            count++;
        }

        System.out.println("Reverse Number is:"+revNum);
        System.out.println("Number of digits is:"+count);
    }
}

9. By Sololearn

Made by Sololearn. Enter input in int number = 86732;. ( Source )

23768
public class Reverse 
{
	public static void main(String[] args) 
	{
		int number = 86732; // sample number
		int reverse = 0;

		while (number > 0) 
		{
			// use modulus operator to strip off the last digit and create the reversed number
			reverse = reverse * 10 + number % 10;
			number /= 10;
		}
		System.out.println(reverse);
	}
}

10. By KODER999

Made by KODER999. ( Source )

5636
6365
/*
#20
validates the user input
#20
checks if the number is greater than 0 else the loop stops๐Ÿ˜€
#22
gets the remainder of the number when divided by 10
#23
prints the remainder
#24
reassigns the number
*/
import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
      Scanner scanner=new Scanner(System.in);
      //accept the number from the user
      int number=scanner.nextInt();
      if (number>0){
          while (number>0){
             int remainder=number%10;
             System.out.print(remainder);
             number=((number-remainder)/10);
              }
         }
   }
}

11. By Purab Gupta

Made by Purab Gupta. Enter any number of any length and get reverse of it. ( Source )

Entered number is 25525
Number after reversing is 52552
Hope you liked it !
import java.util.*;
public class Program
{
    public static void main(String[] args) {
        
        Scanner sc = new Scanner (System.in);
        int a = sc.nextInt();
        
       StringBuffer b=new StringBuffer(Integer.toString(a)).reverse();
       
       System.out.println("Entered number is "+a);
       System.out.println( "Number after reversing is "+b);
       
       System.out.println("Hope you liked it !");
}
}

12. By PurpleGirl

Made by PurpleGirl. ( Source )

123
321
import java.util.Scanner;
public class Reverse
{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        long m=sc.nextInt();
        //int rem;
        
        while(m>0){
         long rem=m%10;
        m=m/10;
        System.out.print(rem);
        }
         
    }
}