The following Java Program uses the Multiplication and Modulus Operator to reverse the number entered by the User. Something like this,
If the entered Number is 1234567, then the final result will display 7654321.
If you do not know about the Operator in Java, click the link below.
Example: Java Program to Reverse the Number using while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //reverse a number in java import java.util.Scanner; public class ReverseNumberJava { public static void main(String args[]) { int n, rev = 0; Scanner in = new Scanner(System.in); System.out.print("Enter an integer to reverse: "); n = in .nextInt(); while (n != 0) { rev = rev * 10; rev = rev + n % 10; n = n / 10; } System.out.println("Reverse of the entered interger is " + rev); } } |
Output of reversing a number:
Enter an integer to reverse: 1234567
Reverse of the entered interger is 7654321