Java reverse number – Java Program to Reverse a Number

Java reverse number: Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Program to Reverse a Number

In this article we will see how we can reverse a number.

What is reverse of a number ?

When we will print a number from backside to front then that number represents the reverse number of an original number.

For example:

153 is the number then 351 is it's reverse.
63984 is the number then 48936 is it's reverse.

That means reverse of a number is the digits put in reverse order.

We are going to see how we can reverse a number in Java in various ways.

Method-1 : Using For Loop

By using for loop we can reverse a number.

Approach:

  • We will take input from the user and store it in a variable.
  • There will be a for loop that runs until the number gets to zero.
  • The statements inside the loop extracts the digits and stores them in the rev variable.
  • The reversed number is printed.

Program:

import java.util.Scanner;
class revNum{
    public static void main(String args[])
    {
        System.out.println("Enter a number");
        //Taking input from user
        Scanner scan = new Scanner(System.in);
        int num=scan.nextInt(), rev=0, digit;
        //For loop that runs until our number gets reduced to 0
        for(;num!=0;num/=10)
        {
            digit = num%10;
            rev = rev *10+ digit;
        }
        System.out.println("The reverse is "+rev);
    }
}
Output:

Enter a number
54321
The reverse is 12345

Method-2 : Using While loop

By using while loop we can reverse a number.

Approach:

  • We will take input from the user and store it in a variable.
  • There is loop that runs until the number gets to zero.
  • The statements inside the loop extracts the digits and stores them in the rev variable.
  • Then the number is divided by 10 in each iteration.
  • The reversed number is printed.

Program:

import java.util.Scanner;
class revNum{
    public static void main(String args[])
    {
        System.out.println("Enter a number");
        //Taking input from user
        Scanner scan = new Scanner(System.in);
        int num=scan.nextInt(), rev=0, digit;
        //While loop that runs unitl our number gets reduced to 0
        while(num>0)
        {
            
            digit = num%10;
            rev = rev *10+ digit;
            num/=10;
        }
        System.out.println("The reverse is "+rev);
    }
}
Output: 

Enter a number 
54321 
The reverse is 12345

Method-3 : Using recursion

By using recursion we can reverse a number.

Approach:

  • We take the input from the user and pass it to the recursive function
  • The recursive function takes the number then divides it by 10 and calls itself until the number gets to 0.
  • Each value is stored in a static variable and according to the positions of the digit.
  • Finally the result is printed.

Program:

import java.util.Scanner;
class revNum{
    static  int rev = 0;
    static  int position = 1;
    public static int reverseNum(int num)
    {
        //Recursive Function
        if (num > 0) {
            reverseNum(num / 10);
            rev += (num % 10) * position;
            position *= 10;
        }
        return rev;
    }
    public static void main(String args[])
    {
        System.out.println("Enter a number");
        //Taking input from user
        Scanner scan = new Scanner(System.in);
        int num=scan.nextInt();
        System.out.println("The reverse is "+reverseNum(num));
    }
}
Output:

Enter a number
546
The reverse is 645

Method-4 : Using library function

By using library function we can reverse a number.

We will be using the java string library to reverse our number here.

Approach:

  • We will take input from the user and store it in a variable.
  • Then we will convert the number to a string so that we can use the reverse function from string library.
  • Then we parse the string as an integer to a variable,
  • The reversed number is printed.

Program:

import java.util.Scanner;
class revNum
{
    public static void main(String args[])
    {
        System.out.println("Enter a number");
        //Taking input from user
        Scanner scan = new Scanner(System.in);
        int num=scan.nextInt(), rev=0, digit;
        
        //Converting the integer into string
        StringBuffer str = new StringBuffer(String.valueOf(num));

        //String reversed using reverse( )
        str.reverse();
        
        // Switching the string back to integer again
        rev = Integer.parseInt(String.valueOf(str));
        System.out.println("The reverse is "+rev);
    }
}
Output:

Enter a number
654
The reverse is 456

Get started with learning the programming language Java from beginner to experienced level by referring to our collection of Java Programs with Source Code and become a pro in the subject.

Related Java Decision Making and Loop Programs: