* 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 class PalindromeNumber {
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;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter your number”);
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.
Tomy is a contributor at AskMeCode. We are committed to providing well-researched, accurate, and valuable content to our readers.
You May Also Like
pdf417, a Technology That is Changing the Way Companies and Stores Manage Inventory
pdf417, a Technology That is Changing the Way Companies and Stores Manage Inventory: A blog about pdf417 and the many...
So you want to learn about pencil code? Start here
So you want to learn about Pencil Code? Start here: a blog that explains how to use Pencil Code. Pencil...
How to Become a Successful Programmer
Becoming a successful programmer is often the result of a lot of hard work, learning, and persistence. And while there...
How to work with Github
Github Codespace is an advanced feature on Github that allows you to run your code or start a project straight...
