Java Program to reverse a number


In this program, You will learn how to reverse a number in Java.


123 => 321

345 => 543


Example: How to reverse a number in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        int n, r, rev = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a Number :");
        n = sc.nextInt();

        while (n > 0) {
            r = n % 10;
            rev = rev * 10 + r;
            n = n / 10;
        }
        System.out.println("\nReverse Number is :" + rev);

    }
}

Output:

Enter a Number :342
Reverse Number is :243