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.
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
- Start.
- Take number in a variable
n
. - Take another variable named
reverse
, to store the reversed number. - Check if
n
is not zero. - If the above condition is true, take the last digit of
n
and append it toreverse
. - Pop out the last digit of
n
. Go to step 4. - Stop.
Java Program
/**
* 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
/**
* 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.
- Start.
- The the number in a variable
n
. - Take another variable reverse, where we shall store reversed number.
- Conver n to string by type casting.
n+""
. - Create a new StringBuilder() object with the string formed in the above step.
- Call reverse() method on the StringBuilder object.
- Call toString() method on the StringBuilder object. toString() returns a reversed string.
- Convert the reversed string to integer using Integer.parseInt().
- Stop.
Java Program
/**
* 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.