Java program to reverse the number entered by the user, then displays the reversed number on the screen.
For example, if the user enters 678 as input, 876 is displayed as output. We use the mod(%) operator in the program to get the digits of an integer.
Java Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; public class JavaExample { public static void main(String args[]){ int n, reverse = 0; System.out.println("Enter an integer :"); Scanner in = new Scanner(System.in); n = in.nextInt(); while(n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; } System.out.println("Reverse of the number is " + reverse); } } |
Output:
1 2 3 4 5 |
Enter an integer : 12345 The number in reversed is = 54321 |