For Loop

Java Palindrome Number Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

15 Comments

  • how cn i show that a number is a palindrome for only counting the number of digits. eg for only 5 digit numbers?

  • the above code is not correct. below is the correct to find the palindrome number:-

    public class JavaPalindromeNumberExample {

    public static void main(String[] args) {

    //array of numbers to be checked

    int numbers[] = new int[]{121,13,34,11,22,54};

    //iterate through the numbers

    for(int i=0; i < numbers.length; i++){

    int number = numbers[i];

    int reversedNumber = 0;

    int temp=0;

    /*

    * If the number is equal to it’s reversed number, then

    * the given number is a palindrome number.

    *

    * For example, 121 is a palindrome number while 12 is not.

    */

    //reverse the number

    while(number > 0){

    temp = number % 10;

    number = number / 10;

    reversedNumber = number* 10 + temp;

    }

    if(numbers[i] == reversedNumber)

    System.out.println(numbers[i] + ” is a palindrome number”);

    else

    System.out.println(numbers[i] + ” is not a palindrome number”);

    }

    }

    }

    // Gaurav Mittal

    • class Star{

      public static void main(String[] ag){

      for(int i=0;i<5;i++){

      for(int j=0;j<i;j++){ system.out.print(“*”);=”” }=”” system.out.println(“*”);=”” }=”” for(int=”” k=”0;k&lt;5;k++){” for(int=”” l=”0;l&lt;4-k;l++){” system.out.print(“*”);=”” }=”” system.out.println();=”” }=”” }=”” }=””>

      • class Test{

        void show(){
        for(int i=0;i<5;i++){

        for(int j=0;j<i;j++){ system.out.print(“*”);=”” }=”” system.out.println(“*”);=”” }=”” for(int=”” k=”0;k&lt;5;k++){” for(int=”” l=”0;l&lt;4-k;l++){” system.out.print(“*”);=”” }=”” system.out.println();=”” }=”” }=”” }=”” public=”” class=”” star=”” extends=”” test{=”” public=”” static=”” void=”” main(string[]=”” ag){=”” star=”” s=”new” star();=”” s.show();=”” }=”” }=””>

    • public class NewClass1 {

      public static void main(String[] args) {

      for(int i=1;i<=5;i++)

      {

      for(int j=1;j<=i;j++)

      {

      if(i==4)

      {

      if(j==3 || j==4 || j==5)

      {

      continue;}}

      if(i==5)

      {

      if(j==2 || j==3 || j==4 || j==5)

      { continue;}}

      System.out.print(“*”);

      }

      System.out.println(“”);

      }

      }

      }

    • public class Palindrome {

      public static void main(String[] args) {

      int m=121;

      int x=m;

      int rev=0;

      while(m!=0){

      rev=rev*10;

      rev=rev+m%10;

      m=m/10;
      }

      if(x==rev){

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

      }

      else

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

      }

      }

  • Can someone validate the following as an alternative simpler implementation:

    class Palindrome {
    public static void main(String… args) {
    long number = 0L;

    try {
    number = new Long(args[0]);
    } catch (NumberFormatException ex) {
    System.out.println(“Enter a valid number.”);
    System.exit(1);
    }

    //excluding negative values and single positive integers
    if (number < 10) {
    System.out.println(“Number is not a palindrome.”);
    return;
    }

    long otherNumber = 0;
    long temp = number;

    while (temp > 0) {
    otherNumber = (otherNumber * 10) + (temp % 10);
    temp = temp / 10;
    }

    if (otherNumber == number) {
    System.out.println(“Number is a palindrome.”);
    } else {
    System.out.println(“Number is not a palindrome”);
    }
    }
    }

Sponsors

Facebook Fans