Java – Reverse a Number

In this tutorial, we shall learn how to write a Java program to reverse a number in Python.

You may use looping statements like For Loop or While to iterate over each digit of the number and reverse the given number.

Another way of reversing a number is that, you can use StringBuilder, and its reverse() method.

Following picture presents a sample input and output for the Java Program – Reverse a Number.

Java Program to Reverse a Number

Example 1 – Reverse Number using While Loop

In this example, we shall reverse a number using Java While Loop.

Following algorithm will be used to reverse a number.

Algorithm

  1. Start.
  2. Take number in a variable n.
  3. Take another variable named reverse, to store the reversed number.
  4. Check if n is not zero.
  5. If the above condition is true, take the last digit of n and append it to reverse.
  6. Pop out the last digit of n. Go to step 4.
  7. Stop.

Java Program

</>
Copy
/**
 * Java Program - Reverse Number
 */

public class Factorial {

	public static void main(String[] args) {
		//number
		int n = 123456789;
		
		//variable to hold reversed number
		int reverse = 0;
		
		//reverse n
		while(n!=0) {
			reverse = (reverse * 10) + n%10; //append last digit of n to reverse
			n /= 10; //pop last digit of n
		}
		
		System.out.println(reverse);
	}
}

Run the above program. After the execution of while loop, reverse contains the reversed number.

987654321

Example 2 – Reverse Number using For Loop

In this example, we shall use Java For Loop to reverse the given number. We will use the same algorithm as in the above example.

Java Program

</>
Copy
/**
 * Java Program - Reverse Number
 */

public class ReverseNumber {

	public static void main(String[] args) {
		//number
		int n = 123456789;
		
		//variable to hold reversed number
		int reverse = 0;
		
		//reverse n
		for(; n!=0; n/=10)
			reverse = (reverse * 10) + n%10;
		
		System.out.println(reverse);
	}
}

Run the above Java program, and you will get the following output in console.

987654321

Example 3 – Reverse Number using StringBuilder.reverse()

In this example, we shall use StringBuilder class and its reverse method to reverse a number.

Algorithm

Following algorithm is used to reverse a number.

  1. Start.
  2. The the number in a variable n.
  3. Take another variable reverse, where we shall store reversed number.
  4. Conver n to string by type casting. n+"".
  5. Create a new StringBuilder() object with the string formed in the above step.
  6. Call reverse() method on the StringBuilder object.
  7. Call toString() method on the StringBuilder object. toString() returns a reversed string.
  8. Convert the reversed string to integer using Integer.parseInt().
  9. Stop.

Java Program

</>
Copy
/**
 * Java Program - Reverse Number
 */

public class ReverseNumber {

	public static void main(String[] args) {
		//number
		int n = 123456789;
		
		//reverse number
		int reverse = Integer.parseInt((new StringBuilder(n+"")).reverse().toString());
		
		System.out.println(reverse);
	}
}

Run the above program. The number is reversed.

987654321

Conclusion

In this Java Tutorial, we learned how to reverse a number using Looping techniques and StringBuilder class.