Palindrome Number Java

Hey guys!! how are you doing…. Welcome to flower brackets blog. In this post we are going to learn palindrome number java.

palindrome number java

Palindrome number

Palindrome number is a number where number remain the same when its numbers are reversed.Palindrome java is the most frequently asked coding question in interview.

For example 121 is a palindrome because it reads the same forwards as well as backwards. While 173 is not a palindrome.

Also Read – Best Way In Java To Swap Two Numbers

Because if you reverse you will read it as 371.

The procedure to approach this problem would be like this,

  1. First we have to reverse the given number.
  2. Then we have to compare the given number with the actual number to check if they both match or not.
  3. If they both match that means the number is palindrome and if it does not match that means the number is not palindrome.

Now let us write a function “javaCheckPalindrome” that takes the number to be checked as input and inside the function we are comparing that number with the output of the function “reverse”.

Here’s the code,

public static boolean javaCheckPalindrome(int number)
{
   return (number == reverse(number));
}

Now “reverse” function will determine the reverse of the number. Here you will compare the output of the “reverse” function with the actual number to check if we have a palindrome or not.

So the actual logic goes inside the function “reverse”. Let’s see the code,

public static int reverse(int number)
{
   int numReverse = 0;
   while(number > 0)
   {
      int a = number % 10;
      numReverse = numReverse * 10 + a;
      number /= 10;
   }
   return numReverse;
}

To calculate the reverse of a number what we have done is, first we have created a variable “numReverse” which will store the reverse number.

And inside the while loop we have written simple logic. First we are dividing a number by 10 and storing the remainder in integer variable “a”.

Now to calculate the reverse number we multiply the existing number by 10 and add the remainder that you just obtained.

Then again for the next iteration we divide our original number by 10.

So let’s say we have an example of 531. So in the first iteration integer variable “a” would be 1, a reverse number would be one and when we divide the number by 10 for the next iteration we have 53.

Also Read – String Palindrome Java

Again variable “a” would be 3 this time. The reverse number would be current reverse number (int numReverse ) which is 1 into 10 plus 3.

So which is 13. Again we divide the number 10. This time variable “a” would be 5 and we will have reverse number and 135.

This function will give you the reverse of the number which we will use to compare with the original number.

Below you can see java program for palindrome number java,

Example: palindrome number java

import java.util.Scanner;

public class PalindromeNumberInJava
{
   public static void main(String[] args)
   {
      System.out.println("Please enter a number to reverse : ");
      Scanner sc = new Scanner(System.in);
      int number = sc.nextInt();
      int x = number;

      // Here number entered is reversed
      int reverse = 0;
      int y;

      while(number > 0)
      {
         y = number % 10;
         reverse = reverse * 10 + y;
         number = number / 10;
      }

      if(reverse == x)
      {
         System.out.println(x + " is a Palindrome Number.");
      }
      else
      {
         System.out.println(x + " is not a Palindrome Number.");
      }
   }
}

Output:

Palindrome Number Java


Using java library

Now let us learn how we can use the methods from java library.

public static int reverse(int number)
{
   StringBuffer strReverse = new StringBuffer(number + "");
   return Integer.parseInt(strReverse().toString());
}

As you can see the function shown above we are passing the number that needs to be checked as an input parameter and inside we are using the functionalities of StringBuffer class.

First off we are converting our number into StringBuffer and storing it in “strReverse” variable.

Now StringBuffer class has a predefined method called reverse, which will automatically reverse the given string.

So let’s say our number is 351. Now when you call reverse on this string stored in the StringBuffer it will automatically give you 153.

Also Read – Random Number Generator Java

It will treat each number as a string character and just reverse it to give you the result. Now when you get the reverse you convert your StringBuffer to a string using the function dot toString().

Why we are converting it to a string? Because we need to convert the string into an integer choosing the function parseInt().

Now what parseInt() function does is it takes a string as input parameter and it will give you the equivalent decimal representation of the number.

Also if you want to convert your string into, say, hexadecimal or octal you can also pass an additional parameter called radix to this function and then it will convert it to whichever radical you want.

Now what parseInt() will do is it will return an integer given a string as input. So you saw how we could easily achieve our solution by using the existing classes, StringBuffer and integer.

We saw the use of the function reverse, toString() and parseInt().

Now let’s see how we can call these functions from the main function,

public static void main(String[] args)
{
   int unit = 353;
   System.out.println(javaCheckPalindrome(unit))
}

We will simply initialize a variable “unit” with the number you want to check and then we are passing it to the function “javaCheckPalindrome”.

Which will finally return the number is palindrome or not. Which in this case is true.


Palindrome number java – using for loop

Let’s learn how to check if a number is a palindrome using for loop. Here’s the example for palindrome program in java.

import java.util.Scanner;

public class PalindromeJava 
{
   public static void main(String[] args) 
   {
      String original, reverse = ""; 
      Scanner sc = new Scanner(System.in); 
      System.out.println("Please enter a string/number to check for palindrome in java : "); 
      original = sc.nextLine(); 
      int length = original.length(); 
      for(int a = length - 1; a >= 0; a--) 
         reverse = reverse + original.charAt(a); 
      if(original.equals(reverse))
      {
         System.out.println(original + " is a palindrome number/string.");
      }
      else
      {
         System.out.println(original + " not a palindrome number/string.");
      }
      sc.close();
   }
}

Output:

Please enter a string/number to check for palindrome in java : mom
mom is a palindrome number/string.


conclusion

That’s it guys. This was all about palindrome number java. I hope you guys have understood how to check if a number is a palindrome in java.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like