Java Program to Check If a Number is Palindrome or Not

Palindrome

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.






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. $reverse \leftarrow 0$

3. $m \leftarrow n$

4. Repeat until $n \neq 0$

5. $reverse \leftarrow reverse * 10 + (n \mod 10)$

6. $n \leftarrow n / 10$

7. If $reverse == m$:

8. Return "Palindrome"

9. Else:

10. Return "Not Palindrome"

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

Code has been copied
/*************************************
          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!



This Java program checks whether a given number is a palindrome or not using iteration. Here's how it works:
  1. The program starts by importing the necessary class java.util.Scanner to read input from the user.
  2. The Main class is declared, which contains the main method where the program execution begins.
  3. Inside the main() method, an instance of the Scanner class is created to read input from the user.
  4. 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.
  5. 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.
  6. The program makes a copy of the original number by assigning n to m.
  7. 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.
  8. After the while loop finishes, the program checks whether the original number m is equal to the reversed number revnum.
  9. If m and revnum are equal, the program outputs that the number is a palindrome using System.out.print.
  10. If m and revnum are not equal, the program outputs that the number is not a palindrome using System.out.print.
  11. The program ends.



5. Java Program & output to check if a number is a palindrome or not using recursion

Code has been copied
/*************************************
          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!



This Java program checks whether a given number is a palindrome or not using recursion. Here's how it works:
  1. The program starts by importing the necessary class java.util.Scanner to read input from the user.
  2. The Main class is declared, which contains the main method where the program execution begins.
  3. 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.
  4. 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.
  5. 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.
  6. Back in the main method, an instance of the Scanner class is created to read input from the user.
  7. An object obj of the Main class is created to access the check_palindrome() function.
  8. A variable n is declared to store the user input.
  9. 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.
  10. 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.
  11. If the check_palindrome() function returns 1, it means the number is a palindrome, and the program outputs the result using System.out.print.
  12. 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.
  13. The program ends.