Thursday, 10 January 2019

Java Program to check the given number is a palindrome or not

      Today's post, I have written java code to check the given number is a palindrome or not. The given number and reverse of that number should be same then it's a palindrome number. 

Example:-

int number = 12121,
reverse of above number is,12121 it should be same, so it's 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