You are here

Java program to reverse a number

This program prints reverse of a number, i.e., if input is 951 then the output will be 159.

Java programming source code

import java.util.Scanner;
class ReverseNumber
   public static void main(String args[])
      int n, reverse = 0;
      System.out.println("Enter a number 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.