Thursday, 10 January 2019

Java Program to Check Whether a Number Is a Palindrome

       In today's post, I have written a Java program to check whether a given number is a palindrome.

A number is called a palindrome if it remains the same when its digits are reversed. In other words, if the original number and its reversed number are identical, then it is a palindrome.

Example

int number = 12121;

The reverse of the above number is 12121, which is the same as the original number. Therefore, it is a palindrome number.

PalindromeNumber.java

package com.example;

class PalindromeNumber{
 
       public static void main(String args[]) {
       
               int number = 12321; 
               int reverse = 0;
               int palindrome = number;
     
               while (number != 0) {
                       int reminder = number % 10;
                       reverse = reverse * 10 + reminder;
                       number = number/10;
               }
     
               if (palindrome == reverse) {
                       System.out.println("The given number "+ palindrome +" is a palindrome");
               } else {
                       System.out.println("The given number "+ palindrome +" is not a palindrome");
               }
        }
}

Output :-- The given number 12321 is a palindrome.



Related Posts:--
1) Write a Java program to print the Floyd's triangle
2) Java Program for Bubble Sort in Ascending and Descending order
3) Java Program to Reverse a Linked List using Recursion and Loops
4) Java Program to Count Occurrence of Word in a Sentence
5) How to Remove duplicates from ArrayList in Java

No comments:

Post a Comment