
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Reverse a Number
For a given input number, write a Java program to reverse its digits. In reverse operation, we print the digits of given number from the last to start. The digit at the last position is swapped with the digit at the first position, the second last digit is swapped with second position digit and so on.
Example Scenario:
Input: number = 123456; Output: rev_number: 654321
How to Reverse a Number in Java?
There are multiple ways to reverse the digits of a given number. We are going to discuss the following:
Using while Loop
To reverse a number using the while loop, we need to run it till the given input number is not equal to 0.
- Find the remainder of the number using the modulo operation by 10.
- It will return the last digit. After that, multiply the reverse number by 10 and add the remainder to shift the digits left.
- Then, divide the number by 10 to remove the last digit from the number.
Example
The following Java program illustrates how to reverse a number using a while loop.
public class ReverseNumber{ public static void main(String[] args){ int my_input , reverse_input; my_input = 123456; reverse_input = 0; System.out.println("The number is defined as " + my_input); while(my_input != 0){ int remainder = my_input % 10; reverse_input = reverse_input * 10 + remainder; my_input = my_input/10; } System.out.println("The reverse value of the given input is: " + reverse_input); } }
When you run this code, following output will be displayed -
The number is defined as 123456 The reverse value of the given input is: 654321
Using reverse() Method
The StringBuffer class of java.lang package provides a method with the name reverse(). This method converts a sequence of characters in reverse order.
But, this method accepts only a string value. So, we need to convert the given integer into a string before passing it to the constructor of the StringBuffer class.
Example
In this example, we are using the reverse() method to reverse the given input number.
public class ReverseNumber { public static void main(String[] args) { int my_input = 8642; System.out.println("The number is defined as:: " + my_input); StringBuffer stringBuf = new StringBuffer(String.valueOf(my_input)); stringBuf.reverse(); int reverse_input = Integer.parseInt(stringBuf.toString()); System.out.println("The reverse value of the given input is:: " + reverse_input); } }
On running this code, the following output will be obtained -
The number is defined as:: 8642 The reverse value of the given input is:: 2468
Using for Loop
To repeat the execution of a code block multiple times, we use a looping statement in Java. For the given problem, we have used a while loop earlier. Now, we are going to see the use of a for loop.
Here, the for loop will start with the existing value of the given number, and the loop will iterate till the number becomes 0. In each iteration, the input number is reduced by removing its last digit.
Example
Let's see the practical implementation of the above logic:
public class ReverseNumber { public static void main(String[] args) { int my_input = 738215, reverse_input = 0; System.out.println("The number is defined as " + my_input); // for loop for (; my_input != 0; my_input /= 10) { int remainder = my_input % 10; reverse_input = reverse_input * 10 + remainder; } System.out.println("The reverse value of the given input is: " + reverse_input); } }
On running this code, it will reverse the given input number and print the result-
The number is defined as 738215 The reverse value of the given input is: 512837