How to Reverse a Number in Java - Java Program to Reverse a Number



How to Reverse a Number in Java :

Sample Code

import java.util.Scanner;

public class reversenumber 
{
  public static void main(String[] args) 
  {
    int n, reverse = 0, rem;
    System.out.println("Enter a number : ");
    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    while (n != 0)
    {
      rem = n % 10;
      reverse = reverse * 10 + rem;
      n /= 10;
    }
    System.out.println("Reversed Number : " + reverse);
  }
}

Output

Enter a number      : 12345
Reversed Number : 54321

Related Searches to How to Reverse a Number in Java - Java Program to Reverse a Number