×

Palindrome number program in Java

Write java program to check if a number is palindrome in Java?

A number that does not change on reversing is called a palindrome number. In other words, when reversing the digits of a number does not affect the number is called the palindrome number. For example, 121, 343, 555, 606, 999, 1001, 1331 are the palindrome numbers. In this section, we will create a Java program to check the number is palindrome or not.

Algorithm to Find Palindrome Number

  • Get the number, which is going to be checked for palindrome.
  • Create a copy of the number by storing it in some temporary variable.
  • Now, reverse the number.
  • Check with the copy whether the reversed number is equal or not.
  • If it is equal, then we have a palindrome number.
  • Otherwise, the number is not a palindrome.

The following Java program demonstrates how to check a given number is a palindrome number or not by using a while loop.

Iterative Approach

Filename: PalindromeNumberExample.java

public class PalindromeNumberExample
{             
public static void main(String[] args)
{
               int no = 535; // To be checked for palindorme
               int tempVar = no; // Creating the copy of ‘no’
               int sum = 0;
               while(no > 0)
               {
                               // reversing the number
                               sum = sum * 10;
                               sum = sum + (no % 10);
                               no = no / 10;
               }
               if(tempVar == sum)
                               System.out.print("The number " + tempVar +" is a palindrome number.");
               else
                               System.out.print("The number " + tempVar +" is not a palindrome number.");
}
}

Output:

The number 535 is a palindrome number.

Explanation: We have used a Java while loop to reverse the number. The number of iterations of the while loop depends on the number of digits present in the given number. In our case, it is 3. Let’s see the working of the program.

First Iteration: sum = sum * 10. However, we have initialized the value of sum as 0; Therefore, we get sum = 0 * 10 = 0. Again, we update the value of sum by adding the remainder we get by dividing the given number (535). Thus, we get sum = 0 + (535 % 10). sum = 0 + 5 = 5. Finally, we divide the given number by 10 and assign the quotient to the number. no = 535 / 10 = 53. The updated value of no variable is 53 which is to be used in the next iteration.

Second Iteration: In the second iteration, the value of sum is 5. Hence, sum = 5 * 10 = 50. Again, we add the remainder. This time the number is 53. Hence, we have sum = 50 + (53 % 10). sum becomes 50 + 3 = 53. At last, the given number shrink again by one digit and becomes 5. no = 53 / 10 = 5.

Third Iteration: The process is similar. The sum is 53. Therefore, sum = 53 * 10 = 530. After adding the remainder, we get 535. Also, the number becomes no = 5/10 = 0. Since every time we have checked the while loop condition. When the condition no>0 returns false, the loop terminates.

Recursive Approach

We can also check the palindrome number by using the recursive approach.

Filename: PalindromeNumberExample1.java

public class PalindromeNumberExample1
{
static boolean isPalindrome(int no, int tempVar, int sum)
{
               if(no < 0) return false;
               if(no == 0) return sum == tempVar;
               return isPalindrome(no/10, tempVar, sum * 10 + (no%10));                  
}
public static void main(String[] args)
{
               int a[] = {4, 434, 909, 100, -66, 123, 797};
               for(int i = 0; i < a.length; i++)
                               if(isPalindrome(a[i], a[i], 0))
                                              System.out.println("The number " + a[i] +" is a palindrome number.");
                               else
                                              System.out.println("The number " + a[i] +" is not a palindrome number.");
}
}

Output:

The number 4 is a palindrome number.
The number 434 is a palindrome number.
The number 909 is a palindrome number.
The number 100 is not a palindrome number.
The number -66 is not a palindrome number.
The number 123 is not a palindrome number.
The number 797 is a palindrome number.

Explanation: In the recursive approach also, we have created a copy of the number and then compared it with the number that we have reversed. Note that as per mathematical definition, a negative number can never be a palindromic number.


Related Topics

Java Pop

The array, linked list, stack, queue, and other data structures are supported by Java programming. The insertion, deletion, and element searching operations are available for every data structure. And Java...

4 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

Java Externalization

What is Externalization in Java? Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine...

3 minutes read.

Java File Input Stream

Java makes use of stream ideas to speed up input and output processes. All packages of input and output streams are contained in java.io.package. Stream: A stream is nothing more...

5 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

Interfaces and Classes in Strings in Java

CharBuffer: CharBuffer is utilized to implement the CharSequence interface. With the help of the mentioned class, we can allow character buffers to be utilized instead of CharSequences. We can consider the illustration...

4 minutes read.

JIT in Java

What is JIT ? JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation...

5 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.