Check if a number is Palindrome
Last Updated :
11 Sep, 2023
Given a positive integer, write a function that returns true if the given number is a palindrome, else false. For example, 12321 is a palindrome, but 1451 is not a palindrome.
Method 1:
Let the given number be num. A simple method for this problem is to first reverse digits of num, then compare the reverse of num with num. If both are same, then return true, else false.
Following is an interesting method inspired from method#2 of this post. The idea is to create a copy of num and recursively pass the copy by reference, and pass num by value. In the recursive calls, divide num by 10 while moving down the recursion tree. While moving up the recursion tree, divide the copy by 10. When they meet in a function for which all child calls are over, the last digit of num will be ith digit from the beginning and the last digit of copy will be ith digit from the end.
C++
#include <iostream>
using namespace std;
int oneDigit( int num)
{
return (num >= 0 && num < 10);
}
bool isPalUtil( int num, int * dupNum)
{
if (oneDigit(num))
return (num == (*dupNum) % 10);
if (!isPalUtil(num / 10, dupNum))
return false ;
*dupNum /= 10;
return (num % 10 == (*dupNum) % 10);
}
int isPal( int num)
{
if (num < 0)
num = -num;
int * dupNum = new int (num);
return isPalUtil(num, dupNum);
}
int main()
{
int n = 12321;
isPal(n) ? cout << "Yes\n" : cout << "No" << endl;
n = 12;
isPal(n) ? cout << "Yes\n" : cout << "No" << endl;
n = 88;
isPal(n) ? cout << "Yes\n" : cout << "No" << endl;
n = 8999;
isPal(n) ? cout << "Yes\n" : cout << "No" ;
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
int oneDigit( int num)
{
return (num >= 0 && num < 10);
}
bool isPalUtil( int num, int * dupNum)
{
if (oneDigit(num))
return (num == (*dupNum) % 10);
if (!isPalUtil(num / 10, dupNum))
return false ;
*dupNum /= 10;
return (num % 10 == (*dupNum) % 10);
}
bool isPal( int num)
{
if (num < 0)
num = -num;
int dupNum = num;
return isPalUtil(num, &dupNum);
}
int main()
{
int n = 12321;
isPal(n) ? printf ( "Yes\n" ) : printf ( "No\n" );
n = 12;
isPal(n) ? printf ( "Yes\n" ) : printf ( "No\n" );
n = 88;
isPal(n) ? printf ( "Yes\n" ) : printf ( "No\n" );
n = 8999;
isPal(n) ? printf ( "Yes\n" ) : printf ( "No\n" );
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class CheckPalindromeNumberRecursion {
public static int oneDigit( int num) {
if ((num >= 0 ) && (num < 10 ))
return 1 ;
else
return 0 ;
}
public static int isPalUtil
( int num, int dupNum) throws Exception {
if (num == 0 ) {
return dupNum;
} else {
dupNum = isPalUtil(num / 10 , dupNum);
}
if (num % 10 == dupNum % 10 ) {
return dupNum / 10 ;
} else {
throw new Exception();
}
}
public static int isPal( int num)
throws Exception {
if (num < 0 )
num = (-num);
int dupNum = (num);
return isPalUtil(num, dupNum);
}
public static void main(String args[]) {
int n = 12421 ;
try {
isPal(n);
System.out.println( "Yes" );
} catch (Exception e) {
System.out.println( "No" );
}
n = 1231 ;
try {
isPal(n);
System.out.println( "Yes" );
} catch (Exception e) {
System.out.println( "No" );
}
n = 12 ;
try {
isPal(n);
System.out.println( "Yes" );
} catch (Exception e) {
System.out.println( "No" );
}
n = 88 ;
try {
isPal(n);
System.out.println( "Yes" );
} catch (Exception e) {
System.out.println( "No" );
}
n = 8999 ;
try {
isPal(n);
System.out.println( "Yes" );
} catch (Exception e) {
System.out.println( "No" );
}
}
}
|
Python3
def oneDigit(num):
return ((num > = 0 ) and
(num < 10 ))
def isPalUtil(num, dupNum):
if oneDigit(num):
return (num = = (dupNum[ 0 ]) % 10 )
if not isPalUtil(num / / 10 , dupNum):
return False
dupNum[ 0 ] = dupNum[ 0 ] / / 10
return (num % 10 = = (dupNum[ 0 ]) % 10 )
def isPal(num):
if (num < 0 ):
num = ( - num)
dupNum = [num]
return isPalUtil(num, dupNum)
n = 12321
if isPal(n):
print ( "Yes" )
else :
print ( "No" )
n = 12
if isPal(n) :
print ( "Yes" )
else :
print ( "No" )
n = 88
if isPal(n) :
print ( "Yes" )
else :
print ( "No" )
n = 8999
if isPal(n) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
public static int oneDigit( int num)
{
if ((num >= 0) &&(num < 10))
return 1;
else
return 0;
}
public static int isPalUtil( int num,
int dupNum)
{
if (oneDigit(num) == 1)
if (num == (dupNum) % 10)
return 1;
else
return 0;
if (isPalUtil(( int )(num / 10), dupNum) == 0)
return -1;
dupNum = ( int )(dupNum / 10);
if (num % 10 == (dupNum) % 10)
return 1;
else
return 0;
}
public static int isPal( int num)
{
if (num < 0)
num = (-num);
int dupNum = (num);
return isPalUtil(num, dupNum);
}
public static void Main()
{
int n = 12321;
if (isPal(n) == 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
n = 12;
if (isPal(n) == 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
n = 88;
if (isPal(n) == 1)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
n = 8999;
if (isPal(n) == 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function oneDigit(num) {
if ((num >= 0) && (num < 10))
return 1;
else
return 0;
}
function isPalUtil
(num , dupNum) {
if (num == 0) {
return dupNum;
} else {
dupNum = isPalUtil(parseInt(num / 10), dupNum);
}
if (num % 10 == dupNum % 10) {
return parseInt(dupNum / 10);
} else {
throw e;
}
}
function isPal(num)
{
if (num < 0)
num = (-num);
var dupNum = (num);
return isPalUtil(num, dupNum);
}
var n = 1242;
try {
isPal(n);
document.write( "<br>Yes" );
} catch (e) {
document.write( "<br>No" );
}
n = 1231;
try {
isPal(n);
document.write( "<br>Yes" );
} catch (e) {
document.write( "<br>No" );
}
n = 12;
try {
isPal(n);
document.write( "<br>Yes" );
} catch (e) {
document.write( "<br>No" );
}
n = 88;
try {
isPal(n);
document.write( "<br>Yes" );
} catch (e) {
document.write( "<br>No" );
}
n = 8999;
try {
isPal(n);
document.write( "<br>Yes" );
} catch (e) {
document.write( "<br>No" );
}
</script>
|
PHP
<?php
function oneDigit( $num )
{
return (( $num >= 0) &&
( $num < 10));
}
function isPalUtil( $num , $dupNum )
{
if (oneDigit( $num ))
return ( $num == ( $dupNum ) % 10);
if (!isPalUtil((int)( $num / 10),
$dupNum ))
return -1;
$dupNum = (int)( $dupNum / 10);
return ( $num % 10 == ( $dupNum ) % 10);
}
function isPal( $num )
{
if ( $num < 0)
$num = (- $num );
$dupNum = ( $num );
return isPalUtil( $num , $dupNum );
}
$n = 12321;
if (isPal( $n ) == 0)
echo "Yes\n" ;
else
echo "No\n" ;
$n = 12;
if (isPal( $n ) == 0)
echo "Yes\n" ;
else
echo "No\n" ;
$n = 88;
if (isPal( $n ) == 1)
echo "Yes\n" ;
else
echo "No\n" ;
$n = 8999;
if (isPal( $n ) == 0)
echo "Yes\n" ;
else
echo "No\n" ;
?>
|
Time Complexity: O(log n)
Auxiliary Space: O(log n)
To check a number is palindrome or not without using any extra space
Method 2:Using string() method
- When the number of digits of that number exceeds 1018, we can’t take that number as an integer since the range of long long int doesn’t satisfy the given number.
- So take input as a string, Run a loop from starting to length/2 and check the first character(numeric) to the last character of the string and second to second last one, and so on ….If any character mismatches, the string wouldn’t be a palindrome.
Below is the implementation of the above approach
C++14
#include <iostream>
using namespace std;
int checkPalindrome(string str)
{
int len = str.length();
for ( int i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1])
return false ;
}
return true ;
}
int main()
{
string st
= "112233445566778899000000998877665544332211" ;
if (checkPalindrome(st) == true )
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG{
static boolean checkPalindrome(String str)
{
int len = str.length();
for ( int i = 0 ; i < len / 2 ; i++)
{
if (str.charAt(i) !=
str.charAt(len - i - 1 ))
return false ;
}
return true ;
}
public static void main(String[] args)
{
String st = "112233445566778899000000998877665544332211" ;
if (checkPalindrome(st) == true )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def checkPalindrome( str ):
for i in range ( 0 , len ( str ) / / 2 ):
if str [i] ! = str [ len ( str ) - i - 1 ]:
return False
return True
st = "112233445566778899000000998877665544332211"
if (checkPalindrome(st) = = True ):
print ( "it is a palindrome" )
else :
print ( "It is not a palindrome" )
|
C#
using System;
class GFG{
static bool checkPalindrome( string str)
{
int len = str.Length;
for ( int i = 0; i < len / 2; i++)
{
if (str[i] != str[len - i - 1])
return false ;
}
return true ;
}
public static void Main()
{
string st = "112233445566778899000000998877665544332211" ;
if (checkPalindrome(st) == true )
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function checkPalindrome(str)
{
var len = str.length;
for ( var i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1])
return false ;
}
return true ;
}
let st
= "112233445566778899000000998877665544332211" ;
if (checkPalindrome(st) == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(|str|)
Auxiliary Space: O(1)
Method 3:
Here is the simplest approach to check if a number is Palindrome or not . This approach can be used when the number of digits in the given number is less than 10^18 because if the number of digits of that number exceeds 10^18, we can’t take that number as an integer since the range of long long int doesn’t satisfy the given number.
To check whether the given number is palindrome or not we will just reverse the digits of the given number and check if the reverse of that number is equal to the original number or not . If reverse of number is equal to that number than the number will be Palindrome else it will not a Palindrome.
C++
#include <iostream>
using namespace std;
bool checkPalindrome( int n)
{
int reverse = 0;
int temp = n;
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = temp / 10;
}
return (reverse
== n);
}
int main()
{
int n = 7007;
if (checkPalindrome(n) == 1) {
cout << "Yes\n" ;
}
else {
cout << "No\n" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean checkPalindrome( int n)
{
int reverse = 0 ;
int temp = n;
while (temp != 0 ) {
reverse = (reverse * 10 ) + (temp % 10 );
temp = temp / 10 ;
}
return (reverse == n);
}
public static void main(String args[])
{
int n = 7007 ;
if (checkPalindrome(n) == true ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
def checkPalindrome(n):
reverse = 0
temp = n
while (temp ! = 0 ):
reverse = (reverse * 10 ) + (temp % 10 )
temp = temp / / 10
return (reverse = = n)
n = 7007
if (checkPalindrome(n) = = 1 ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool checkPalindrome( int n)
{
int reverse = 0;
int temp = n;
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = temp / 10;
}
return (
reverse
== n);
}
public static void Main( string [] args)
{
int n = 7007;
if (checkPalindrome(n) == true ) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function checkPalindrome(n)
{
let reverse = 0;
let temp = n;
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = Math.floor(temp / 10);
}
return (reverse == n);
}
let n = 7007;
if (checkPalindrome(n) == 1) {
document.write( "Yes" , "</br>" );
}
else {
document.write( "No" , "</br>" );
}
</script>
|
Time Complexity : O(log10(n)) or O(Number of digits in a given number)
Auxiliary space : O(1) or constant
This article is compiled by Aashish Barnwal.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...