Categories: softare development

Check If a Number is Palindrome in Java

It is true that Strings can be palindromic in nature. The same can equally be said of numbers.

A palindromic number (sometimes called numeral or numeric palindrome) is one such that if its digits are reversed, it stays the same.

This number is said to have a reflectional symmetry across the vertical axis. Example of such numbers are 16461, 151, 242, etc.

Let us now see how we can check for this palindromic quality using Java programming language.

class CheckPalindrome{

public static void main(Strings args[]){
  System.out.println(isPalindromeInt(121));
}

public static String isPalindromeInt(int n) {
   
   int reversedNum = 0;
   int remainder;
   int originalNum = n;
    
         while (n != 0) {
      remainder = num % 10;
      reversedNum = reversedNum * 10 + remainder;
      n /= 10;
    }
    
    // check if reversedNum and originalNum are equal
    if (originalNum == reversedNum) {
 return originalNum + "is Palindrome.";
    }
    else {
    return originalNum + "is not Palindrome.";
    }
  }

}

So what we did here was to create a static method that will return our given result based on the parameters passed.

By using the modulus operator, we effectively get the remainder when we divide by 10 and then attempt to do a reversal.

If the reversed number is still equal to the reversed number, then it is a palindrome, otherwise it is not.

This is effectively how Palindrome works.

Let us know what you think in the comments.

Recent Posts

Instagram Extends Reels Duration to 3 Minutes

Regardless of whether TikTok faces a U.S. ban, Instagram is wasting no time positioning itself…

1 day ago

AWS Expands Payment Options for Nigerian Customers, Introducing Naira (NGN) for Local Transactions

Amazon Web Services (AWS) continues to enhance its customer experience by offering more flexible payment…

5 days ago

Why JavaScript Remains Dominant in 2025

JavaScript, often hailed as the "language of the web," continues to dominate the programming landscape…

7 days ago

Amazon Moves to Upgrade Alexa with Generative AI Technology

Amazon is accelerating efforts to reinvent Alexa as a generative AI-powered “agent” capable of performing…

1 week ago

Smuggled Starlink Devices Allegedly Used to Bypass India’s Internet Shutdown

SpaceX's satellite-based Starlink, which is currently unlicensed for use in India, is reportedly being utilized…

1 week ago

Why Netflix Dumped React For its Frontend

Netflix, a pioneer in the streaming industry, has always been at the forefront of adopting…

1 week ago