Java Program to Reverse a Number

In this example, we are giving a program to reverse a number.


To reverse a number:

  1. # To reverse a number
  2. public class ReverseNumber {
  3. public static void main(String[] args) {
  4. int num = 1234, reversed = 0;
  5. while(num != 0) {
  6. int digit = num % 10;
  7. reversed = reversed * 10 + digit;
  8. num /= 10;
  9. }
  10. System.out.println("Reversed Number: " + reversed);
  11. }
  12. }

Output:

Reversed Number: 4321