Home » Blog » Check Number Is Palindrome Or Not

Check Number Is Palindrome Or Not

A Palindromic Number is a number that is the same when written forwards or backwards.The first few palindromic numbers are therefore are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, … etc.

 

Let’s take one number from user. Then assign it to another temporary variable for performing operation purpose. Now take this temporary variable and execute the loop till this temp variable is not zero. Inside this loop take last digit of the number by finding modulus of the number and store to another variable, after that append the that variable to final answer rev variable by using formula¬† rev = (rev * 10) + m; here first time the rev is 0, at last take division by 10 of the temp variable so that the last digit is removed.

Now in rev variable the reverse format of number is obtained. Now compare it with original number of it matches then number is palindrome otherwise number is not palindrome.

import java.util.*;
public class palindrom
{
	public static void main(String args[])
	{	
		Scanner sc=new Scanner(System.in);
		int temp,no,rev=0,m;
		System.out.println("***Palindrom Number***");
		System.out.println("Enter any number =");
		no=sc.nextInt();
		temp=no;

		while(temp!=0)
		{
			m=temp%10;
			rev=(rev*10)+m;
			temp=temp/10;
		}
		if(no==rev)
		{
			System.out.println("Number is Palindrom");
		}
		else
		{
			System.out.println("Number is not palindrom");
		}
	}
}

Here is the OutPut

***Palindrom Number***
Enter any number =
123
Number is not palindrom


***Palindrom Number***
Enter any number =
121
Number is Palindrom

 

Tags:

Leave a Reply

Your email address will not be published.