Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaPalindrome Number Program in Java

Palindrome Number Program in Java

A given number can be said palindromic in nature if the reverse of the given number is the same as that of a given number. In this article, we will write a Program to check if a number is a Palindrome Number in Java.

Example of Palindrome Number

Input : n = 46355364
Output: Reverse of n = 46355364
Palindrome : Yes

Input : n = 12345678999999999987654321
Output: Reverse of n = 12345678999999999987654321
Palindrome : Yes

Methods to Check If a Number is Palindrome Number in Java

There are certain methods to check if a Number is a Palindrome Number in Java as mentioned below:

  1. Using Iterative Approach
  2. Using Recursive Approach
  3. Using BigInteger Class
  4. Using String

1. Check for Palindrome Number using Iteration

We can reverse a number in multiple ways, below is the iterative implementation for the same.

Algorithm

Iterative Algorithm when the given input is an integer number.

  1. Initialize reversed_number = 0
  2. Loop while number > 0
    • Multiply reversed_number by 10 and add number % 10 to reversed_number
      • reversed_number = reversed_number*10 + number %10;
      • Divide the number by 10
  3. Return reversed_number

Program to Check for Palindrome Numbers in Java

Java




// Java program to reverse a number
// and find if it is a palindrome or not
  
// Driver Class
class GFG {
    // Iterative function to
    // reverse the digits of number
    static int reversNumber(int n)
    {
        int reversed_n = 0;
        while (n > 0) {
            reversed_n = reversed_n * 10 + n % 10;
            n = n / 10;
        }
        return reversed_n;
    }
  
    // Main function
    public static void main(String[] args)
    {
        int n = 123464321;
        int reverseN = reversNumber(n);
        System.out.println("Reverse of n = " + reverseN);
  
        // Checking if n is same
        // as reverse of n
        if (n == reverseN)
            System.out.println("Palindrome = Yes");
        else
            System.out.println("Palindrome = No");
    }
}


Output

Reverse of n = 123464321
Palindrome = Yes

The complexity of the above method

Time Complexity: O(log10(n)) where n is the input number.

Auxiliary space: O(1) because constant variables have been used

2. Check for Palindrome Numbers using Recursion

The idea is to solve this problem by using recursion in a different way, Below are the steps:

  1. Define a recursive method recursive_func().
  2. It should have two parameters as the given number N and the resultant reverse number as rev.
  3. Recursively reversing the number until N becomes less than 10 (base condition).
  4. Finally returning the reversed number.

Program to Check for Palindrome Numbers in Java

Java




// Java program to reverse a number
// and find if it is a palindrome or not
  
// Driver Class
class GFG {
  
    // Recursive function to reverse
    // the digits of number
    static int recursive_func(int n, int rev)
    {
        if (n < 10) {
            return rev * 10 + n;
        }
        else {
            int last_digit = n % 10;
            rev = rev * 10 + last_digit;
            return recursive_func(n / 10, rev);
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 123464321;
        int rev = recursive_func(n, 0);
  
        System.out.println("Reverse of n = " + rev);
  
        // Checking if n is same
        // as reverse of n
        if (n == rev)
            System.out.println("Palindrome = Yes");
        else
            System.out.println("Palindrome = No");
    }
}


Output

Reverse of n = 123464321
Palindrome = Yes

The complexity of the above method is mentioned below:

Time Complexity: O(log10N)

Space Complexity: O(n) n-no of digits in a number [stack space]

3. Check for Palindrome Number using BigInteger Class

Big Integer can’t be operated using the same as the normal Integer so we need to use BigInteger Class to solve the issue faced like Overflowing of the values. The length of the number is log10(n), i.e. For BigIntegers using string operations like creation reverse and check palindrome will take log10(n) time. 

Approach

  1. Take input in BigInteger variable.
  2. Reverse the given BigInteger using the reverse method.
  3. Compare both BigIntegers using compareTo() method.

Program to Check for Palindrome Numbers in Java

Java




// Java program to reverse a number
// and find if it is a palindrome or not
import java.io.*;
import java.math.BigInteger;
  
// Driver Class
class GFG {
    // Reverse Big Integer
    public static BigInteger reverse(BigInteger n)
    {
        String s = n.toString();
        StringBuilder sb = new StringBuilder(s);
        return new BigInteger(sb.reverse().toString());
    }
  
    // Main Function
    public static void main(String[] args)
    {
        BigInteger n
            = new BigInteger("12345678999999999987654321");
        BigInteger reverseN = reverse(n);
        System.out.println("Reverse of n = " + reverseN);
  
        // Checking if n is same
        // as reverse of n
        if (n.compareTo(reverseN) == 0)
            System.out.println("Palindrome = Yes");
        else
            System.out.println("Palindrome = No");
    }
}


Output

Reverse of n = 12345678999999999987654321
Palindrome = Yes

Time Complexity: O(log10(n)) where n is the input number.

Auxiliary Space: O(n) for StringBuilder

4. Using String to Check if a Number is Palindrome

We can convert a number into String, and after conversion, we can use the functions available with Strings for conversions.

Program to Check for Palindrome Numbers in Java

Java




// Java Program to Check if a Number
// is Palindrome using String Class
import java.io.*;
import java.util.*;
  
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        String s;
  
        // Taking input using Scanner Class
        Scanner in = new Scanner(System.in);
  
        System.out.print("Enter Number = ");
        // Storing the the input value in s
        s = in.nextLine();
  
        // Length of the String
        int n = s.length();
        String rev = "";
  
        for (int i = n - 1; i >= 0; i--) {
            rev = rev + s.charAt(i);
        }
  
        // Printing the reversed Number
        System.out.println("Reverse Number = " + rev);
  
        // Checking Palindrome
        if (s.equals(rev))
            System.out.println("Palindrome = Yes");
        else
            System.out.println("Palindrome = No");
    }
}


Input:

Enter Number = 12345654321

Output:

Reverse Number = 12345654321
Palindrome = Yes

For more information, please refer to the complete article here.

RELATED ARTICLES

Most Popular

Recent Comments