Java Program to Find Reverse Number of a Given Number

Program to Find Reverse Number of a Given Number

In this article we are going to understand what Reverse number is and how we can check whether a number is Reverse or not in Java with examples.

Reverse numbers are numbers formed after reversing the digits

 Example :

19 =>91
46 =>64

In the above examples the reverse of 19 and 46 are 91 and 64 respectively.

Approach :

  1. We ask the user to enter a number and store it.
  2. We iterate through its digits inside a loop and store them in another variable.
  3. The reverse number inside the variable is printed.

Let’s see different ways to find reverse of a number.

Method-1: By Using Static Value

import java.util.Scanner;
public class ReverseNumber
{
    public static void main(String args[])
    {
        //number declared;
        int num = 67;

        int rev=0,remainder,temp=num;
        while(temp>0)
        {
            remainder=temp%10;
            rev = rev*10+remainder;
            temp=temp/10;
        }

        System.out.println("The reverse of "+num+" is "+rev);
    }
}
Output:

The reverse of 67 is 76

Method-2: By User Input Value

import java.util.Scanner;
public class ReverseNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        int rev=0,remainder,temp=num;
        while(temp>0)
        {
            remainder=temp%10;
            rev = rev*10+remainder;
            temp=temp/10;
        }

        System.out.println("The reverse of "+num+" is "+rev);
    }
}
Output:

Enter a number : 45
The reverse of 45 is 54

Leave a Comment