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.

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

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

Output:

Enter a number:234
Reverse number:432