How to reverse any number using core java concepts

In this example we are going to write a java program which will enable us to enter any integer value using Scanner object. In while loop we are going to reverse the entered number.


import java.util.Scanner;

class ReverseNumber {
    public static void main(String args[]) {
        int original, reverse = 0;

        System.out.print("Enter the number to reverse: ");
        Scanner in = new Scanner(System.in);
        original = in.nextInt();

        while (original != 0) {
            reverse = reverse * 10;
            reverse = reverse + original % 10;
            original = original / 10;
        }

        System.out.println("Reverse of entered number is: " + reverse);
    }
}


Output:

Enter the number to reverse: 123
Reverse of entered number is: 321

1 comment:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

    or learn thru JavaScript Online Training from India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

    ReplyDelete