You are here

Reverse a number in Java

Java program to find the reverse of a number, for example, if the input is 951, the output is 159.

Java program to reverse a number

import java.util.Scanner;

class ReverseNumber
  public static void main(String args[])
    int n, reverse = 0;

    System.out.println("Enter an integer to reverse");
    Scanner in = new Scanner(;
    n = in.nextInt();

    while(n != 0)
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;

    System.out.println("Reverse of the number is " + reverse);

Download Reverse number program class file.

Output of program:
reverse number

You can also reverse or invert a number using recursion. You can use this code to check if a number is a palindrome number or not. If on reversing a number, we get the same number, then it's a palindrome number; otherwise, not.