Java programs to check if a number is a palindrome or not have been shown here. For example, 35653 is a palindrome because we get the same number irrespective of the direction (left to right or right to left) of reading. But 1234 is not a palindrome as we get 4321 if we read it from right to left.
The algorithm, time complexity and pseudocode of the program have been shown below.
Page content(s):
1. Algorithm to check if a number is a palindrome or not
1. Take a number n as input.
2. Copy value of n to another variable say m i.e. m = n.
3. Initialize a variable to 0 say reverse = 0.
4. Perform r = n % 10.
5. Perform reverse = reverse * 10 + r.
6. Perform n = n / 10
7. If n = 0 go to step 8 else go to step 3.
8. Check If reverse == m
9. If step 8 is true then n is palindrome else n is not palindrome.
2. Pseudocode to check if a number is a palindrome or not
Input : A number $n$
Output : Palindrome or Not Palindrome
1. Procedure checkPalindrome($n$):
2.
3.
4.
5.
6.
7.
8.
9.
10.
11. End Procedure
3. Time complexity to check if a number is a palindrome or not
Time Complexity: O(log(n))
Where n is the input number.
4. Java Program & output to check if a number is a palindrome or not using iteration
/************************************* alphabetacoder.com Java program to check if a number is a palindrome or not using iteration **************************************/ import java.util.Scanner; class Main { public static void main(String args[]) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int n, m, revnum = 0, r; // take input System.out.print("Enter the number = "); n = sc.nextInt(); // copy the number m = n; //find the reverse while (n != 0) { // extract the unit digit r = n % 10; // store the reverse number // give appropriate positional value // of each digit revnum = revnum * 10 + r; //divide the number by 10 n = n / 10; } // check for palindrome if (m == revnum) System.out.print(m + " is a palindrome!"); else System.out.print(m + " is not a palindrome!"); } }
Output
Case 1:
Enter the number = 7890987
7890987 is a palindrome!
Case 2:
Enter the number = 1234
1234 is not a palindrome!
- The program starts by importing the necessary class java.util.Scanner to read input from the user.
- The Main class is declared, which contains the main method where the program execution begins.
- Inside the main() method, an instance of the Scanner class is created to read input from the user.
- Several variables are declared:
- n represents the original number entered by the user.
- m is a copy of the original number, which will be used later for comparison.
- revnum is initially set to 0 and will store the reverse of the number.
- r represents the remainder when dividing the number by 10, which gives us the unit digit of the number.
- The program prompts the user to enter a number using System.out.print and reads the input using sc.nextInt(), storing it in the variable n.
- The program makes a copy of the original number by assigning n to m.
- Using a while loop, the program calculates the reverse of the number by performing the following steps until n becomes 0:
- Extract the unit digit of the number by calculating the remainder r when dividing n by 10.
- Update the revnum by multiplying it by 10 and adding the extracted unit digit r.
- Divide the number n by 10 to remove the unit digit.
- After the while loop finishes, the program checks whether the original number m is equal to the reversed number revnum.
- If m and revnum are equal, the program outputs that the number is a palindrome using System.out.print.
- If m and revnum are not equal, the program outputs that the number is not a palindrome using System.out.print.
- The program ends.
5. Java Program & output to check if a number is a palindrome or not using recursion
/************************************* alphabetacoder.com Java program to check if a number is a palindrome or not using recursion **************************************/ import java.util.Scanner; class Main { // recursive function to check if // a number is a palindrome or not public int check_palindrome(int n, int num, int rev) { if (n == 0) { // reverse is same as original if (num == rev) return 1; else return 0; } else return check_palindrome(n / 10, num, (rev * 10 + n % 10)); } public static void main(String args[]) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare object of Main class Main obj = new Main(); // declare variables int n; // take input System.out.print("Enter the number = "); n = sc.nextInt(); // check for palindrome // by calling the function // If the function returns 1, // then the number is palindrome // else the number is not palindrome if (obj.check_palindrome(n, n, 0) == 1) System.out.print(n + " is a palindrome!"); else System.out.print(n + " is not a palindrome!"); } }
Output
Enter the number = -3050503
-3050503 is a palindrome!
- The program starts by importing the necessary class java.util.Scanner to read input from the user.
- The Main class is declared, which contains the main method where the program execution begins.
- Inside the Main class, a recursive function named check_palindrome() is defined. This function takes three parameters:
- n represents the number that needs to be checked for palindrome.
- num represents the original number that remains unchanged throughout recursion.
- rev stores the reverse of the number.
- Inside the check_palindrome() function, the first condition checks if n is equal to 0. If it is, it means all the digits of the original number have been processed, and the function proceeds to check whether the num is equal to the rev (reversed number). If they are equal, the function returns 1 to indicate that the number is a palindrome. Otherwise, it returns 0 to indicate that the number is not a palindrome.
- If n is not equal to 0, the function makes a recursive call to itself with updated values:
- n / 10 removes the last digit from n.
- num remains unchanged as it represents the original number.
- (rev * 10 + n % 10) calculates the reverse of the number by multiplying the current rev by 10 and adding the last digit of n.
- Back in the main method, an instance of the Scanner class is created to read input from the user.
- An object obj of the Main class is created to access the check_palindrome() function.
- A variable n is declared to store the user input.
- The program prompts the user to enter a number using System.out.print and reads the input using sc.nextInt(), storing it in the variable n.
- The program checks whether the entered number n is a palindrome by calling the check_palindrome() function on obj. It passes the initial values of n, n, and 0 as arguments to the function.
- If the check_palindrome() function returns 1, it means the number is a palindrome, and the program outputs the result using System.out.print.
- If the check_palindrome() function returns 0, it means the number is not a palindrome, and the program outputs the result using System.out.print.
- The program ends.