
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
Reverse an Integer in Java
To reverse an integer in Java, try the following code −
Example
import java.lang.*; public class Demo { public static void main(String[] args) { int i = 239, rev = 0; System.out.println("Original: " + i); while(i != 0) { int digit = i % 10; rev = rev * 10 + digit; i /= 10; } System.out.println("Reversed: " + rev); } }
Output
Original: 239 Reversed: 932
In the above program, we have the following int value, which we will reverse.
int i = 239;
Now, loop through until the value is 0. Find the remainder and perform the following operations to get the reverse of the given integer 239.
while(i != 0) { int digit = i % 10; rev = rev * 10 + digit; i /= 10; }
Advertisements