×

Java Programs

Java Practice

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

    me@linux:~$ javac Palindrome.java 

    First Run:
    me@linux:~$ java Palindrome
    Enter any integer number: 12321
    12321 is a Palindrome Number.

    Second Run:
    me@linux:~$ 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

    me@linux:~$ javac Palindrome.java 

    First Run:
    me@linux:~$ java Palindrome
    Enter any integer number: 12321
    12321 is a Palindrome Number.

    Second Run:
    me@linux:~$ java Palindrome
    Enter any integer number: 1234
    1234 is not a Palindrome Number.

Core Java Example Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.