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.

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("Reverse number:" + rev);

    }
}

Output:

Enter a number:145
Reverse number:541