# Palindrome Number

/**

* Java program to check if number is Palindrome.

* A palindrome number is a number that remains the same when its digits are reversed.

*/

public class Palindrome {

public static void main(String args[]){

System.out.println(“Is 121 palindrome? ” + isPalindrome(121));

System.out.println(“Is 12321 palindrome? ” + isPalindrome(12321));

System.out.println(“Is 1212 palindrome? ” + isPalindrome(1212));

//another way to check if int number is palindrome or not in java

System.out.println(“Is 121 palindrome? ” + isPalindrome2(121));

System.out.println(“Is 12321 palindrome? ” + isPalindrome2(12321));

System.out.println(“Is 1212 palindrome? ” + isPalindrome2(1212));

}

/** Java Program to check if given number is Palindrom or not */

public static boolean isPalindrome

public static void main(String[] args) {

System.out.println(“Enter the number to check if it is a palindrome”);

Scanner sc = new Scanner(System.in);

int number = sc.nextInt();

boolean flag = false;

int temp = number, reverse = 0;

while (temp != 0) {

reverse = reverse * 10 + temp % 10;

temp /= 10;

}

if (number == reverse) {

flag = true;

}

if (flag == true) {

System.out.println(“The given number is a palindrome.”);

} else {

System.out.println(“The given number is not a palindrome.”);

}

} // main method ends here

} //class ends here

A palindrome number is a number that is equal to its reverse. For example, 121 is a palindrome because it is equal to its reverse 121. Similarly, the number 565 is also a palindrome because it is equal to its reverse 565.

In this program we first take a number from user and store it in an integer variable num. Then we use the while loop to iterate until num > 0. In each iteration of while loop, we extract the last digit of user entered number by using modulus operator % (num % 10). After that we multiply the reverse variable by 10 and add last digit to reverse like:

reverse = (reverse * 10) + lastDigit;

import java.util.Scanner;

public class Palindrome{

public static void main(String[] args){

int r,sum=0,temp;

Scanner sc = new Scanner(System.in);

System.out.println(“enter value :”);

int n=sc.nextInt();

temp=n;

while(n>0){

r=n%10;

sum=(sum*10)+r;

n=n/10;

}

if(temp==sum)

System.out.println(“palindrome number “);

else

System.out.println(“not palindrome”);

}

}

import java.io.*;

class PalindromeExample{

public static void main(String args[])throws IOException{

int r,sum=0,temp;

int n=Integer.parseInt(in.readLine()); //It is the number variable to be checked for palindrome

temp=n;

while(n>0){

r=n%10; //getting remainder

sum=(sum*10)+r;

n=n/10;

}

if(temp==sum)

System.out.println(“palindrome number “);

else

System.out.println(“not palindrome”);

}

}

Save the below program as Palindrome.java.

import java.util.*;

public class Palindrome {

public static void main(String[] args) {

int num = 121, reversedInteger = 0, remainder, originalInteger;

originalInteger = num;

// reversed integer is stored in variable

while( num != 0 )

{

remainder = num % 10;

reversedInteger = reversedInteger * 10 + remainder;

num /= 10;

}

if (originalInteger == reversedInteger) System.out.println(originalInteger + ” is a palindrome.”);

else System.out.println(originalInteger + ” is not a palindrome.”);

}

}

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. Examples of numerical palindromes are:

23322

110011

54322345

For a given number num, write a function to test if it’s a numerical palindrome or not and return a boolean (true if it is and false if not). Return “Not valid” if the input is not an integer or less than 0.