logo image

Reverse a given number in Java



Java program to to reverse a given number. Ex. If the input is 123 it prints 321

import java.util.Scanner;

public class ReverseNumber {
	public static void main(String args[]) {
		
		int input;
		int result = 0;

		System.out.println("Please enter a number");

		Scanner in = new Scanner(System.in);
		input = in.nextInt();
		in.close();

		while (input != 0) {
			result = result * 10;
			result = result + input % 10;
			input = input / 10;
		}

		System.out.println("Reverse of the given number is: " + result);
	}

}

Output:

Please enter a number
34
Reverse of the given number is: 43