Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program To Check Palindrome Number

In this tutorial, we will learn how to check whether the entered number is palindrome or not. A palindrome number is a number or a string that remains unaltered when written backward. But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java

Input: Enter the number: 87876

Output: The entered number 87876 is not a palindrome number.

Program 1: Check Palindrome in Java

In the below program, we will see how to check whether the entered number is palindrome or not.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a string variable.

  4. Ask the user to initialize the variable.

  5. Declare a variable to store the reverse number.

  6. Initialize it to a null string.

  7. Use a for loop for the same.

  8. Check whether the reversed number/string is the same as the original number/string or not.

  9. If the same, then print it as a palindrome number.

  10. If not the same, then print it as not a palindrome number.

  11. Display the result.

  12. Stop.

Below is the Java code for finding palindrome numbers.

// Java program to find palindrome number
import java.util.*;
public class Main 
{  
   public static void main(String[] args) 
   {  
       //Take input from the user
       //Create instance of the Scanner class
       Scanner sc = new Scanner(System.in);  
       System.out.println("Enter the number: ");  
       String reverse = ""; 
       String num = sc.nextLine(); 
       int length = num.length();   
      for ( int i = length - 1; i >= 0; i-- )  
         reverse = reverse + num.charAt(i);  
      if (num.equals(reverse))  
         System.out.println("The entered string " +num +" is a palindrome.");  
      else  
         System.out.println("The entered string " +num +"  isn't a palindrome.");     
  }
} 


Enter the number: 212
The entered string 212 is a palindrome.

Program 2: Check Palindrome Number in Java

In the below program, we will see how to check whether the entered number is palindrome or not.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable.

  4. Ask the user to initialize the variable.

  5. Declare a variable to store the reverse number.

  6. Initialize it to 0.

  7. Use a while loop for the same.

  8. Check whether the reversed number is the same as the original number or not.

  9. If the same, then print it as a palindrome number.

  10. If not the same, then print it as not a palindrome number.

  11. Display the result.

  12. Stop.

Below is the Java code for finding palindrome numbers.

// Java program to find palindrome number
import java.util.*;
public class Main 
{  
   public static void main(String[] args) 
   {  
       //Take input from the user
       //Create instance of the Scanner class
       Scanner sc = new Scanner(System.in);  
       System.out.println("Enter the number: ");  
       int num=sc.nextInt();
       int r,sum=0;
       int temp=num;    
       while(num>0)
       {    
       r=num%10;    
       sum=(sum*10)+r;    
       num=num/10;    
       }    
        if(temp==sum)    
        System.out.println("The entered number "+temp+" is a palindrome number ");    
        else    
        System.out.println("The entered number "+temp+" is not a palindrome");    
  }  
} 


Enter the number: 8338
The entered number 8338 is a palindrome number

Program 3: Check Palindrome Number in Java

In the below program, we will see how to check whether the entered number is palindrome or not.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a range.

  4. Ask the user to initialize the range.

  5. Call a method that will check whether palindrome or not.

  6. Declare a variable to store the reverse number.

  7. Initialize it to 0.

  8. Use a while loop for the same.

  9. Check whether the reversed number is the same as the original number or not.

  10. If the same, then return true.

  11. If not the same, then return false.

  12. Display the result.

  13. Stop.

Below is the Java code for finding palindrome numbers.

// Java program to find palindrome number
import java.util.*;
public class Main 
{  
   public static void main(String[] args) 
   {  
       //Take input from the user
       //Create instance of the Scanner class
       Scanner sc = new Scanner(System.in);  
       System.out.println("Enter the number: ");  
       int num1=sc.nextInt();
       System.out.println("Enter the number: ");  
       int num2=sc.nextInt();
       for(int i=num1;i<=num2;i++)
       {
           boolean temp=checkPalindrome(i);
           if(checkPalindrome(i))
           System.out.print(i+" ");
       }
        
  }
  public static boolean checkPalindrome(int num)
  {
      int r,sum=0;
       int temp=num;    
       while(num>0)
       {    
       r=num%10;    
       sum=(sum*10)+r;    
       num=num/10;    
       }    
        if(temp==sum)    
        return true;  
        else    
        return false;
  } 
} 


Enter the number: 10
Enter the number: 500
11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 404 414 424 434 444 454 464 474 484 494