Home > Java > javaTutorial > body text

Java program to reverse a number and check if it is a palindrome

WBOY
Release: 2023-08-26 15:09:13
forward
1422 people have browsed it

Java program to reverse a number and check if it is a palindrome

What is a palindrome number?

If given a number (two, three or four digits), reverse the position of each number from front to back, and then vice versa, and then whether the output number after reversing all elements is the same then it It's a palindrome number.

  • String - String is a storage capsule or a storage method by which we can store a sequence of characters in a Java program.

  • Array - An array is a collection of similar types of data placed in different locations in a sequential form, making it easier to calculate where the data is in a program by simply adding it to a survey basic data.

Let us understand this through an example -

Given Input: a = 102022220201
Output : Reverse of a = 102022220201
//The Number Is Palindrome = YES//
Copy after login
Given Input: b =20011997
Output : Reverse of b = 79911002
//The Number Is Palimdrome = NO//
Copy after login

Algorithm - Reverse numbers and their palindromes

This is a general algorithm for reversing a number and finding if it is a palindrome -

  • Step 1 - Check the number of palindromes.

  • Step 2 - The number should be saved in a non-persistent variable.

  • Step 3 - Change the numbers.

  • Step 4 - Now check the number in the temporary variable with the reversed number.

  • Step 5- If both numbers remain the same or identical, then "it is a palindrome number".

  • Step 6 - Either/or "This is not".

Syntax: Part 1 - Reverse Numbers

{
   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;
}
Copy after login

Syntax: Part 2 - Finding 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.");
}
Copy after login

Let us look at the palindrome program in Java. This would be the simplest form of a palindrome program.

The following methods are very useful for checking palindrome numbers -

  • Check if a number is a palindrome by using strings and arrays.

  • Find out palindrome number by using string in Java.

  • By checking whether the array is a palindrome.

Checking numeric palindromes by using strings and arrays

Steps to check if a number is a palindrome -

  • Initialization of flag not set or flag value is 0.

  • Set the array size to n/2.

  • Check the conditions of the loop. Then set the flag value to 1.

  • Interruption.

  • The loop ends and the result is obtained.

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");
      }
   }
}
Copy after login

Output

Enter a number to test
1111
the number is not palindrome
Copy after login

Find the number of palindromes in Java by using a 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 exmaple 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.");
      }
   }
}
Copy after login

Output

Radar2022 -is not a Palindrome String.
Copy after login

By checking whether the array is a palindrome

There are two procedures to check whether the array is a palindrome.

  • Returns true if there is only one character in the string.

  • Otherwise, just compare the first and last characters 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);
   }
}
Copy after login

Output

The String Is Not Palindrome
Copy after login

in conclusion

After definitions and some theory-based procedures, it can be easily said that a palindrome is a number that remains unchanged even after changing the original number.

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

The above is the detailed content of Java program to reverse a number and check if it is a palindrome. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template