Find out wheather the given number is palindrome or not.

Here we are going to slightly modify the previous program of reversing the number. By adding the new variable and if else condition, here we are going to determine whether the given number is palindrome or not.

import java.util.Scanner;

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

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

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

        if (originalCopy == reverse)
            System.out.println("The entered number is palindrome.");
        else
            System.out
            .println("The entered number is not a palindrome! Please try with different value like 1221 etc...");
    }
}


Output:

Enter the number to reverse: 123
The entered number is not a palindrome! Please try with different value like 1221 etc...

No comments:

Post a Comment