Java – How to Reverse a Number ?

In this article, we will write a simple Java program to reverse a number

1. Reverse a Number

  • We will reverse a number using 2 different loops,
    1. using while loop
    2. using for loop

1.1 Reverse a Number using while-loop

  • Declare a temporary variable called reversed which stores partial reversed number in every iterations
  • Iterate using while loop by checking whether number is greater-than Zero
  • Inside while loop, in each iteration update reversed variable by summing 2 things
    • 1st multiply current reversed value with 10
    • 2nd get remainder of the current number after modulo by 10
    • For next iteration, get new number by dividing current number by 10
  • Finally, return reversed number to the calling method for printing to console

ReverseNumberUsingWhileLoop.java

package in.bench.resources.numbers;

public class ReverseNumberUsingWhileLoop {

	// main() method
	public static void main(String[] args) {

		// lets reverse year 1987
		System.out.println("The reverse of number 1987 is = " 
				+ reverseNumber(1987));


		// lets reverse year 2023
		System.out.println("The reverse of number 2023 is = " 
				+ reverseNumber(2023));
	}


	/**
	 * This method is used to reverse number using while-loop
	 * 
	 * @param number
	 * @return
	 */
	public static int reverseNumber(int number) {

		// local variable
		int reversed = 0;

		while(number != 0) {

			// build reverse number by iterating
			reversed = (reversed * 10) + (number % 10);

			// get next number
			number = number / 10;
		}

		// return reversed number
		return reversed;
	}
}

Output:

The reverse of number 1987 is = 7891
The reverse of number 2023 is = 3202

1.2 Reverse a Number using for-loop

  • Declare a temporary variable called reversed which stores partial reversed number in every iterations
  • Iterate using for-loop
    • 1st assign temp number with given/input number
    • 2nd put condition by checking whether number is greater-than Zero
    • 3rd to get new number for next iteration, divide current number by 10
  • Inside for-loop, in each iteration update reversed variable by summing 2 things
    • 1st multiply current reversed value with 10
    • 2nd get remainder of the current number after modulo by 10
  • Finally, return reversed number to the calling method for printing to console

ReverseNumberUsingForLoop.java

package in.bench.resources.numbers;

public class ReverseNumberUsingForLoop {

	// main() method
	public static void main(String[] args) {

		// lets reverse year 1999
		System.out.println("The reverse of number 1999 is = " 
				+ reverseNumber(1999));


		// lets reverse year 2014
		System.out.println("The reverse of number 2014 is = " 
				+ reverseNumber(2014));
	}


	/**
	 * This method is used to reverse number  using for-loop
	 * 
	 * @param input
	 * @return
	 */
	public static int reverseNumber(int input) {

		// local variable
		int reversed = 0;


		for(int num = input; num != 0; num = num / 10) {

			// build reverse number by iterating
			reversed = (reversed * 10) + (num % 10);
		}

		// return reversed number
		return reversed;
	}
}

Output:

The reverse of number 1999 is = 9991
The reverse of number 2014 is = 4102

Happy Coding !!
Happy Learning !!

Java – Check whether number is Prime or Not ?
Java - Check whether number is Even or Odd ?