Java Program to reverse a number using class and object


In this program, You will learn how to reverse a number using class and object in Java.


123 => 321

456 => 654


Example: How to reverse a number using class and object in Java

Run it Now
import java.util.Scanner;

class Main {

    int n, rev = 0, r;
    Scanner sc = new Scanner(System.in);

    void input() {
        System.out.print("Enter a Number :");
        n = sc.nextInt();
    }

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

    public static void main(String args[]) {
        Main rr = new Main();
        rr.input();
        rr.reverse();
    }
}

Output:

Enter a Number :182
The Reverse Number is :281