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 = 1234561111111111654321
Output: Reverse of n = 1234561111111111654321
Palindrome : Yes
Methods to Check If a Number is a Palindrome Number in Java
There are certain methods to check if a Number is a Palindrome Number in Java as mentioned below:
- Using Iterative Approach
- Using Recursive Approach
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.
- Initialize reversed_number = 0
- 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
- Return reversed_number
Program to Check for Palindrome Numbers in Java
Java
class GFG {
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;
}
public static void main(String[] args)
{
int n = 123464321 ;
int reverseN = reversNumber(n);
System.out.println( "Reverse of n = " + reverseN);
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:
- Define a recursive method recursive_func().
- It should have two parameters as the given number N and the resultant reverse number as rev.
- Recursively reversing the number until N becomes less than 10 (base condition).
- Finally returning the reversed number.
Below is the implementation for Palindrome Numbers in Java using Recursion:
Java
class GFG {
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);
}
}
public static void main(String[] args)
{
int n = 123464321 ;
int rev = recursive_func(n, 0 );
System.out.println( "Reverse of n = " + rev);
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]
Palindrome BigInteger Number
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 to Check Palindrome for BigInteger:
- Take input in BigInteger variable.
- Reverse the given BigInteger using the reverse method.
- Compare both BigIntegers using compareTo() method.
Below is the implementation to check if a BigInteger is Palindrom or not:
Java
import java.io.*;
import java.math.BigInteger;
class GFG {
public static BigInteger reverse(BigInteger n)
{
String s = n.toString();
StringBuilder sb = new StringBuilder(s);
return new BigInteger(sb.reverse().toString());
}
public static void main(String[] args)
{
BigInteger n
= new BigInteger( "12345678999999999987654321" );
BigInteger reverseN = reverse(n);
System.out.println( "Reverse of n = " + reverseN);
if (n.compareTo(reverseN) == 0 )
System.out.println( "Palindrome = Yes" );
else
System.out.println( "Palindrome = No" );
}
}
|
Output
Reverse of n = 12345678999999999987654321
Palindrome = Yes
Below is the complexity of the method mentioned above:
Time Complexity: O(log10(n)) where n is the input number.
Auxiliary Space: O(n) for StringBuilder
String Palindrome Number
Although the point is not fully justified as the string number is itself a string so It can be said to check is a string is palindrome or not ,and the method to check so is mentioned below.
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 is mentioned below:
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
String s;
Scanner in = new Scanner(System.in);
System.out.print( "Enter Number = " );
s = in.nextLine();
int n = s.length();
String rev = "" ;
for ( int i = n - 1 ; i >= 0 ; i--) {
rev = rev + s.charAt(i);
}
System.out.println( "Reverse Number = " + rev);
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 for Palindrome Number Program.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!