Reverse a Number and Check if it is a Palindrome in Java



What is a Palindrome Number?

If a number is given (two, three or four-digit number) and reversing each individual number with the positions from front to back and then vice versa , and then if the number output is the same after reversing all the elements then it is said to be a palindrome number.

Just like we will check whether the string or array is palindrome number.

  • String - A string is a storing capsule or a storing method in which we can store a sequence of characters in a Java program.

  • Array - An array is a collection of similar type of data placed in different positions in a sequential form which makes it easier to calculate the position of the data on the program by simply adding with the base data in the survey.

Problem Statement

Given a number check if it is a palindrome number or not.

Input 1

Given Input: a = 102022220201

Output

Output : Reverse of a = 102022220201
//The Number Is Palindrome = YES//

Input 2

Given Input: b =20011997

Output

Output : Reverse of b = 79911002
//The Number Is Palindrome = NO//

Checking Palindrome By Reversing A Number

Here is the general algorithm to reverse a number and find if it is a palindrome or not ?

  • Step 1 ? Check for a palindrome number.

  • Step 2 ? The number should be held in a non-permanent variable .

  • Step 3 ? Change the number.

  • Step 4 ? Now check the number which will be in the temporary variable with the reversed number .

  • Step 5 ? If the two numbers remain identical or same then it's a palindrome number.

  • Step 6 ? Either or else it's not.

Syntax: Part 1 - To Reverse A Number

{
   int a,res=0,m;
   Scanner sc=new Scanner(System.in);
   System.out.println("Enter a number which you want to be reversed");
}
m=sc.nextInt();
for( ;m!=0; ) {
   a=m%10;
   res=(res*10)+a;
   m=m/10;
}

Syntax: Part 2 - To Find The Palindrome Of A Number

if (originalNum1 == reversedNum1) {
   System.out.println(originalNum1 + " is a Palindrome number.");
}
else {
   System.out.println(originalNum1 + " is not a Palindrome number.");
}

Let us see the palindrome program in Java . This will be the simplest form of palindrome program.

Below approaches are useful for checking the palindrome number ?

Checking a Number Palindrome or not by Using String and Array

Steps to follow to check whether the number is palindrome or not ?

  • Initialization of a flag to unset or flag value is 0.

  • Set the array size to n/2.

  • Check the loop for the condition. Then set the flag value to 1.

  • Break.

  • Loop end with the result.

Example

import java.util.*;
public class palindromeTest{
   public static void main(String args[]){
      int n;
      System.out.println("Enter a number to test");
      Scanner sc = new Scanner (System.in);
      n=sc.nextInt();
      int r=0 ,q=0 ,num=0 ,p=0;
      num= n;
      while(n!=0){
         r = r%10;
         q = n/10;
         p = p*10+r ;
         n = q;
      }
      if (num==p){
         System.out.println("the number is a palindrome number");
      }
      else{
         System.out.print("the number is not palindrome");
      }
   }
}

Output

Enter a number to test
1111
the number is not palindrome

Finding out the Palindrome Number in Java Using String ?

A palindrome number if get reversed, it will return the same number as the input provided. Using the StringBuffer method with the one way reverse(), we can check the palindrome number by using Java. In below example, we will check whether the input string is palindrome or not.

Example

public class Main {
   public static void main(String[] args) {

      String str = "Radar2022", reverseStr2 = "";
      int strLength1 = str.length();
      for (int i = (strLength1 - 1); i >=0; --i) {
      reverseStr2 = reverseStr2 + str.charAt(i);
      }
      if (str.toLowerCase().equals(reverseStr2.toLowerCase())) {
         System.out.println(str + "  -is a Palindrome String.");
      }
      else {
         System.out.println(str + " -is not a Palindrome String.");
      }
   }
}

Output

Radar2022 -is not a Palindrome String.

Checking the Array is Palindrome or not

There are two process to check whether the array is palindrome or not.

  • If only one char present in the string, the return will be true.

  • Else, just compare the first and last chars and put them in a recursive method.

Example

public class findapalindrome {

   static void palindrome(int arr[], int n){
      int flag7 = 0;
      for (int i = 0; i <= n / 2 && n != 0; i++) {
         if (arr[i] != arr[n - i - 1]) {
            flag7 = 1;
            break;
         }
      }
      if (flag7 == 1)
      System.out.println("The String Is Not Palindrome");
      else
      System.out.println("The String Is Palindrome");
   }
   public static void main(String[] args){
      int arr[] = { 100, 200, 300, 222, 1111 };
      int n = arr.length;
      palindrome(arr, n);
   }
}

Output

The String Is Not Palindrome

Conclusion

After going through the definitions and some theory based programs it can be easily said that a palindrome number is a type of number which persists unchanged even after changing the original number.

All these explanations are properly described with examples and for better understanding we have provided sample programs as well based on three different methods.

Updated on: 2024-12-13T21:52:25+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements