Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to reverse a number in Java ?.



Program to demonstrate how to reverse a number in Java



 
import java.util.Scanner;

public class ReverseNumber {

 public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in); 

  System.out.print("Enter a number to reverse : "); 

  int original = scanner.nextInt();
  int reverse = 0;
  int remainder;

  while(original != 0) { 

   remainder = original % 10; 
   reverse = reverse * 10 + remainder; 
   original = original / 10; 

  }

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


 }

}


Output of the program : 

 
 
© 2021 Learn Java by Examples Template by Hubberspot