Write a Java Program to check whether a given number is Palindrome or not.
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.
Examples 272, 515, 71417.
1. Enter the number as an input.
2. Reverse the number.
3. Check if the reversed number is equal to the original number or not.
4. If they are equal, print the number is a Palindrome number else print, the number is not a palindrome number.
There are several ways to check whether a number is Palindrome or not in Java. Let’s look at all different approaches to program this function:
Palindrome Program in Java
String Palindrome Program in Java
In this approach, we check for Palindrome number using while loop.
Here is the source code of the Java Program to Reverse a Number & Check if it is a Palindrome using While loop. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/* * Java Program to Check Number is Palindrome or Not using While Loop. */ import java.util.Scanner; public class Palindrome { public static void main(String args[]) { int n, m, rev = 0, x; Scanner s = new Scanner(System.in); System.out.print("Enter the number:"); n = s.nextInt(); m = n; while(n > 0) { x = n % 10; rev = rev * 10 + x; n = n / 10; } if(rev == m) { System.out.println(" "+m+" is a palindrome number"); } else { System.out.println(" "+m+" is not a palindrome number"); } } }
1. Take the number, n as input from the user.
2. Initialize the variable m and store the value of n in it and also initialize rev=0.
3. Run a while loop till n is greater than 0.
4. Inside the while loop reverse the number n by using the formula rev=rev*10 + n%10 and in each iteration recalculate the value of n as n/10.
5. After reversing the number and storing it in the variable rev check if m is equal to rev or not.
6. If m is equal to rev print the number as palindrome number.
7. Else, print the number as not a palindrome number.
Time Complexity: O(log n)
The above program for checking palindrome number has a time complexity of O(n) as inside the while loop the value of n reduces to n/10 in each iteration.
Space Complexity: O(1)
In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.
Testcase 1: In this case, we enter the number “515” as input to check whether it is a palindrome number or not.
Enter the number: 515 515 is a palindrome number.
Testcase 2: In this case, we enter the number “145” as input to check whether the entered number is palindrome number or not.
Enter the number: 145 145 is not a palindrome number.
In this approach, we check for Palindrome number using recursion.
Here is the source code of the Java program to check whether a number is palindrome or not using recursion. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.
/* *Java Program to check number is Palindrome or Not using recursion */ import java.util.Scanner; public class Main { public static void main (String[]args) { int num, reverse = 0, rem, temp; Scanner s = new Scanner(System.in); System.out.print("Enter the number:"); num = s.nextInt(); if (isPalindrome(num, reverse) == num) System.out.println (num + " is a Palindrome"); else System.out.println (num + " is not a Palindrome"); } static int isPalindrome(int num, int rev) { if(num == 0) return rev; int rem = num % 10; rev = rev * 10 + rem; return isPalindrome(num / 10, rev); } }
Time Complexity: O(log n)
The above program for checking palindrome number has a time complexity of O(n) as the recursive function is called the number of times n is divided by 10.
Space Complexity: O(1)
In the above palindrome program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.
Testcase 1: In this case, we enter the number “1551” as input to check whether it is a palindrome number or not.
Enter the number: 1551 1551 is a palindrome number.
Testcase 2: In this case, we enter the number “537” as input to check whether the entered number is palindrome number or not.
Enter the number: 537 537 is not a palindrome number.
To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.
- Practice Information Technology MCQs
- Practice Programming MCQs
- Practice BCA MCQs
- Apply for Java Internship
- Apply for Computer Science Internship