Home » Java programs » Java basic programs

Java program for Palindrome Number - check whether Number is Palindrome or Not.

Check Palindrome Number Example in Java - This program will read an integer number and check whether given number is Palindrome or Not, In this program we will check that number is equal to reverse number than number is palindrome.

Check Palindrome Number using Java program

//Java program for Palindrome Number.
 
import java.util.*;
 
public class Palindrome
{
    public static void main(String args[]){
        int num,tNum,sum;
         
        Scanner bf=new Scanner(System.in);
         
        //input an integer number
        System.out.print("Enter any integer number: ");
        num= bf.nextInt();
         
        //find reverse number
        tNum=num;
        sum =0;
        while(tNum>0)
        {
            sum = (sum*10) + (tNum%10);
            tNum/=10;
        }
         
        //check inputted number with reversed number
        if(num==sum)
            System.out.println(num + " is a Palindrome Number.");
        else
            System.out.println(num + " is not a Palindrome Number.");
    }
}

Output

    [email protected]:~$ javac Palindrome.java 

    First Run:
    [email protected]:~$ java Palindrome
    Enter any integer number: 12321
    12321 is a Palindrome Number.

    Second Run:
    [email protected]:~$ java Palindrome
    Enter any integer number: 1234
    1234 is not a Palindrome Number.

Using function/Method

//Java program for Palindrome Number.
 
import java.util.*;
 
public class Palindrome
{
    //function to check number is Palindrome or not
    public static boolean isPalindrome(int num)
    {
        int sum,tNum;
        //find reverse number
        tNum=num;
        sum =0;
        while(tNum>0)
        {
            sum = (sum*10) + (tNum%10);
            tNum/=10;
        }
         
        if(num==sum)
            return true;
        else
            return false;
    }
     
    public static void main(String args[]){
        int num;
         
        Scanner bf=new Scanner(System.in);
         
        //input an integer number
        System.out.print("Enter any integer number: ");
        num= bf.nextInt();
                 
        if(isPalindrome(num))
            System.out.println(num + " is a Palindrome Number.");
        else
            System.out.println(num + " is not a Palindrome Number.");
    }
}

Output

    [email protected]:~$ javac Palindrome.java 

    First Run:
    [email protected]:~$ java Palindrome
    Enter any integer number: 12321
    12321 is a Palindrome Number.

    Second Run:
    [email protected]:~$ java Palindrome
    Enter any integer number: 1234
    1234 is not a Palindrome Number.




Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.