Dremendo Tag Line

Input a number and check it is a palindrome number or not using while loop in Java

while Loop - Question 14

In this question, we will see how to input a number and check if it is a palindrome number or not in Java programming using while loop. To know more about while loop click on the while loop lesson.

Q14) Write a program in Java to input a number and check if it is a palindrome number or not using while loop.

Palindrome numbers are such numbers whose reverse is same as the original number. Example, 121, 14541, 22 etc.

Program

import java.util.Scanner;

public class Q14
{
    public static void main(String args[])
    {
        int n,d,on,rev=0;
        Scanner sc=new Scanner(System.in);

        System.out.print("Enter any number ");
        n=sc.nextInt();
        on=n;			// storing the original number

        while(n>0)
        {
            d=n%10;
            rev=(rev*10)+d;		// reverse the number
            n=n/10;
        }

        if(on==rev)
        {
            System.out.println("Palindrome Number");
        }
        else
        {
            System.out.println("Not Palindrome Number");
        }
    }
}

Output

Enter any number 14541
Palindrome Number
video-poster