Saturday, September 1, 2012

Palindrome: Java program to check number is palindrome or not

How to find if number is palindrome in Java
Checking whether a number is palindrome or not is a classical Java homework exercise. When you start learning Java programming most institute which teach Java  programming provides these classical Java programs like How to find Armstrong number in Java or how to find Fibonacci number in Java using recursion etc. Write a Java program to check if number is palindrome comes from same category. For those who are not familiar with palindrome numbers, palindrome number is a number which is equal to reverse of itself. For example 121 is a palindrome because reverse of 121 is 121, while 123 is not a palindrome in Java because reverse of 123 is 321 and 121!=321. Finding palindrome number is easy in Java, you just need to develop logic to reverse a number in Java. Thankfully Java provides couple of arithmetic operators like remainder(%) operator also known as modules operator, which returns remainder in division operator and division operator(/) which returns quotient. By using remainder and division operator in Java we can create program to check if number is palindrome or not.

 

Java program – palindrome numbers in Java

Write a Java program to check if number is palindrome or notHere is a complete Java program to check if a given number is palindrome or not, This program works for both positive and negative numbers and display if its palindrome irrespective of there sign. Any one digit number including zero is always palindrome. This program is able to check two digit , three digit numbers for palindrome and effective go to any number in Java. Let’s see how to write Java program to find palindrome numbers :



package testing;
import java.util.Scanner;
/**
 * Java program to check if number is palindrome or not.
 * A number is called palindrome if number and its reverse is equal
 * This Java program can also be used to reverse a number in Java
 */

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");
        }      
       
    }
 
    /*
     * Java method to check if number is palindrome or not
     */

    public static boolean isPalindrome(int number) {
        int palindrome = number; // copied number into variable
        int reverse = 0;

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

        // if original and reverse of number is equal means
        // number is palindrome in Java
        if (number == reverse) {
            return true;
        }
        return false;
    }

}

Output:
Please Enter a number :
123
Number : 123 is not a palindrome
Please Enter a number :
121
Number : 123 is a palindrome


That’s all on how to check if a number is palindrome or not in Java. If you are looking answer of this question for interview purpose then better prepare a recursive version of palindrome program in Java as well because its common practice that programmer are expected to write java program using both recursion and iteration in Java. Let me know if you find any other way of checking palindrome in Java.

Other Java homework programming exercise:

7 comments:

  1. Very good Program!
    Please correct the out put when it asks 121 is a palindrome....the result says
    123" is a palindrome... it should be 121 :)
    Cheers!

    ReplyDelete
  2. Hi, Thanks for this. The program is working well but I am trying to understand how the reverse(sort of where it came from) fits the program.

    ReplyDelete
  3. // Variations, using charAt

    public static boolean isPolindrome(int n) {

    String s = String.valueOf(n).toString();
    String rs = new StringBuffer(s).reverse().toString();

    for (int i=0; i<s.length(); i++) {
    if (s.charAt(i) != rs.charAt(i)) return false;
    }

    return true;
    }

    ReplyDelete
  4. Hi you dont even need to reverse the string .

    static boolean checkAlgoOne(int num) {

    boolean check = false;

    String number = Integer.toString(num);
    char[] chars = number.toCharArray();
    int len = chars.length;

    if (num < 0)
    check = false;
    if (num == 0 || num == 1)
    check = true;

    if (len % 2 != 1) {
    check = false;
    } else {

    for (int i = 0; i <= len / 2; i++) {
    if (chars[i] == chars[len - 1])
    check = true;
    }

    }
    return check;
    }

    cheers!

    ReplyDelete
    Replies
    1. 1221 is palindrome, but your program will say it's not

      Delete
  5. My way:
    /* Palindrome Program In JAVA
    © Code Nirvana (www.codenirvana.in)
    */
    import java.util.Scanner;
    class Palindrome{
    public static void main(String args[]){
    System.out.print("Enter Number: ");
    Scanner read = new Scanner(System.in);
    int num = read.nextInt();
    int n = num;
    //reversing number
    int rev=0,rmd;
    while(num > 0)
    {
    rmd = num % 10;
    rev = rev * 10 + rmd;
    num = num / 10;
    }
    if(rev == n)
    System.out.println(n+" is a Palindrome Number!");
    else
    System.out.println(n+" is not a Palindrome Number!");
    }
    }

    MORE @ http://www.codenirvana.in/2013/10/Palindrome-Number-in-JAVA.html

    ReplyDelete
  6. private static boolean isPalindrome(int n) {
    return n == Integer.valueOf(new StringBuilder(String.valueOf(n)).reverse().toString());
    }

    ReplyDelete

Java67 Headline Animator