Palindrome number

Palindrome number is a number which is equal to the reverse of itself (or) the number which is equal to the reverse of itself is called as a palindrome number. Determining if a number is palindrome or not is similar to the logic which is used to reverse a number. This program involves division and modulus operator.

Algorithm :

  • Get the number to check for palindrome
  • Hold the number in temporary variable
  • Reverse the number
  • Compare the temporary number with reversed number
  • If both numbers are same, print “palindrome number”
  • Else print “not palindrome number”

Program to determine palindrome number

import java.util.Scanner;

public class NoClassDefFoundErrorDueToStaticInitFailure {

public static void main(String args[]){
 
 System.out.println(“Please Enter a number : “);
 int palindrome = new Scanner(System.in).nextInt();
 
 if(isPalindrome(palindrome)){
 System.out.println(“Number : “ + palindrome + “ is a palindrome”);
 }else{
 System.out.println(“Number : “ + palindrome + “ is not a palindrome”);
 } 
 
 }

public static boolean isPalindrome(int number) {
 int palindrome = number; 
 int reverse = 0;

while (palindrome != 0) {
 int remainder = palindrome % 10;
 reverse = reverse * 10 + remainder;
 palindrome = palindrome / 10;
 }

if (number == reverse) {
 return true;
 }
 return false;
 }

}
Output : enter a number

121

number : 121 is a palindrome.

Determining palindrome number or not using for loop

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.