Java program to check whether a number is palindrome or not

Do you want to check whether a number is a palindrome number or not using java? If your answer is Yes then you are in the right place. In this tutorial, I will guide you point to point to check the palindrome numbers in Java. So let’s start.

What is a palindrome number?

Before starting we have to know what is the palindrome number. If you reverse a number and get the same number then it will palindrome number. Suppose take number 121, Now reverse this number. Our reserve number is also 121. We can see after reversing we get the same number. so 121 is a palindrome number.

Now look at 222, and reverse this number. After reversing you will same number. So 222 is also a palindrome number.

Java logic for a palindrome number check

At first, we take a number from the user. We store this number in a variable. Then we take a temporary variable and assign it to the original variable. Now we reverse our temporary variable. After reversing we check our original variable. If our reverse number matches wir original number, then we can call this a palindrome number. Otherwise, this is not a palindrome number.

Java program for Palindrome number

package com.mycompany.test;

import java.util.Scanner;

public class Test  {

    public static void main(String[] args) {
        
     int num, reverse=0,temp, rem;
     //Call user to input a number
     System.out.println("Enter a number");
     // To get input we have to use java Scanner Class
     Scanner scanner=new Scanner(System.in);
     num=scanner.nextInt();
     //input number is complet
     
     temp=num;
     //reverse number
     while(temp!=0){
      rem=temp%10;
      reverse=reverse*10+rem;
      temp=temp/10;
     }
     // check revers number and original number
     if(num==reverse){
       System.out.println("This is a palindrome number");
     }else{
      System.out.println("This is not palindrome nubmer");
     }
      
    }
}

Output

Enter a number
121
This is a palindrome number
Enter a number
122
This is not palindrome nubmer

I hope you understand this java program. Using this java program you can easily find out whether any number that is palindrome or not.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *