Check if a number is Palindrome
Given an integer, write a function that returns true if the given number is palindrome, else false. For example, 12321 is palindrome, but 1451 is not palindrome.

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++
// A recursive C++ program to check whether a given number is // palindrome or not #include <stdio.h> // A function that reurns true only if num contains one digit int oneDigit(int num) { // comparison operation is faster than division operation. // So using following instead of "return num / 10 == 0;" return (num >= 0 && num < 10); } // A recursive function to find out whether num is palindrome // or not. Initially, dupNum contains address of a copy of num. bool isPalUtil(int num, int* dupNum) { // Base case (needed for recursion termination): This statement // mainly compares the first digit with the last digit if (oneDigit(num)) return (num == (*dupNum) % 10); // This is the key line in this method. Note that all recursive // calls have a separate copy of num, but they all share same copy // of *dupNum. We divide num while moving up the recursion tree if (!isPalUtil(num/10, dupNum)) return false; // The following statements are executed when we move up the // recursion call tree *dupNum /= 10; // At this point, if num%10 contains i'th digit from beiginning, // then (*dupNum)%10 contains i'th digit from end return (num % 10 == (*dupNum) % 10); } // The main function that uses recursive function isPalUtil() to // find out whether num is palindrome or not int isPal(int num) { // If num is negative, make it positive if (num < 0) num = -num; // Create a separate copy of num, so that modifications made // to address dupNum don't change the input number. int *dupNum = new int(num); // *dupNum = num return isPalUtil(num, dupNum); } // Driver program to test above functions int main() { int n = 12321; isPal(n)? printf("Yesn"): printf("Non"); n = 12; isPal(n)? printf("Yesn"): printf("Non"); n = 88; isPal(n)? printf("Yesn"): printf("Non"); n = 8999; isPal(n)? printf("Yesn"): printf("Non"); return 0; } |
Java
// A recursive Java program to // check whether a given number // is palindrome or not import java.io.*; import java.util.*; class GFG { // A function that reurns true // only if num contains one digit public static int oneDigit(int num) { // comparison operation is // faster than division // operation. So using // following instead of // "return num / 10 == 0;" if((num >= 0) &&(num < 10)) return 1; else return 0; } // A recursive function to // find out whether num is // palindrome or not. // Initially, dupNum contains // address of a copy of num. public static int isPalUtil(int num, int dupNum) { // Base case (needed for recursion // termination): This statement // mainly compares the first digit // with the last digit if (oneDigit(num) == 1) if(num == (dupNum) % 10) return 1; else return 0; // This is the key line in // this method. Note that // all recursive calls have // a separate copy of num, // but they all share same // copy of *dupNum. We divide // num while moving up the // recursion tree if (isPalUtil((int)(num / 10), dupNum) == 0) return -1; // The following statements // are executed when we move // up the recursion call tree dupNum = (int)(dupNum / 10); // At this point, if num%10 // contains i'th digit from // beiginning, then (*dupNum)%10 // contains i'th digit from end if(num % 10 == (dupNum) % 10) return 1; else return 0; } // The main function that uses // recursive function isPalUtil() // to find out whether num is // palindrome or not public static int isPal(int num) { // If num is negative, // make it positive if (num < 0) num = (-num); // Create a separate copy // of num, so that modifications // made to address dupNum // don't change the input number. int dupNum = (num); // *dupNum = num return isPalUtil(num, dupNum); } // Driver Code public static void main(String args[]) { int n = 12321; if(isPal(n) == 0) System.out.println("Yes"); else System.out.println("No"); n = 12; if(isPal(n) == 0) System.out.println("Yes"); else System.out.println( "No"); n = 88; if(isPal(n) == 1) System.out.println("Yes"); else System.out.println( "No"); n = 8999; if(isPal(n) == 0) System.out.println("Yes"); else System.out.println( "No"); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
Python3
# A recursive Pyhton3 program to check # whether a given number is palindrome or not # A function that reurns true # only if num contains one digit def oneDigit(num): # comparison operation is faster # than division operation. So # using following instead of # "return num / 10 == 0;" return ((num >= 0) and (num < 10)); # A recursive function to find # out whether num is palindrome # or not. Initially, dupNum # contains address of a copy of num. def isPalUtil(num, dupNum): # Base case (needed for recursion # termination): This statement # mainly compares the first digit # with the last digit if (oneDigit(num)): return (num == (dupNum) % 10); # This is the key line in this # method. Note that all recursive # calls have a separate copy of # num, but they all share same # copy of *dupNum. We divide num # while moving up the recursion tree if (isPalUtil(int(num / 10), dupNum) == False): return -1; # The following statements are # executed when we move up the # recursion call tree dupNum = int(dupNum / 10); # At this point, if num%10 # contains i'th digit from # beiginning, then (*dupNum)%10 # contains i'th digit from end return (num % 10 == (dupNum) % 10); # The main function that uses # recursive function isPalUtil() # to find out whether num is # palindrome or not def isPal(num): # If num is negative, # make it positive if (num < 0): num = (-num); # Create a separate copy of # num, so that modifications # made to address dupNum # don't change the input number. dupNum = (num); # *dupNum = num return isPalUtil(num, dupNum); # Driver Code n = 12321; if(isPal(n) == 0): print("Yes"); else: print("No"); n = 12; if(isPal(n) == 0): print("Yes"); else: print("No"); n = 88; if(isPal(n) == 1): print("Yes"); else: print("No"); n = 8999; if(isPal(n) == 0): print("Yes"); else: print("No"); # This code is contributed by mits |
C#
// A recursive C# program to // check whether a given number // is palindrome or not using System; class GFG { // A function that reurns true // only if num contains one digit public static int oneDigit(int num) { // comparison operation is // faster than division // operation. So using // following instead of // "return num / 10 == 0;" if((num >= 0) &&(num < 10)) return 1; else return 0; } // A recursive function to // find out whether num is // palindrome or not. // Initially, dupNum contains // address of a copy of num. public static int isPalUtil(int num, int dupNum) { // Base case (needed for recursion // termination): This statement // mainly compares the first digit // with the last digit if (oneDigit(num) == 1) if(num == (dupNum) % 10) return 1; else return 0; // This is the key line in // this method. Note that // all recursive calls have // a separate copy of num, // but they all share same // copy of *dupNum. We divide // num while moving up the // recursion tree if (isPalUtil((int)(num / 10), dupNum) == 0) return -1; // The following statements // are executed when we move // up the recursion call tree dupNum = (int)(dupNum / 10); // At this point, if num%10 // contains i'th digit from // beiginning, then (*dupNum)%10 // contains i'th digit from end if(num % 10 == (dupNum) % 10) return 1; else return 0; } // The main function that uses // recursive function isPalUtil() // to find out whether num is // palindrome or not public static int isPal(int num) { // If num is negative, // make it positive if (num < 0) num = (-num); // Create a separate copy // of num, so that modifications // made to address dupNum // don't change the input number. int dupNum = (num); // *dupNum = num return isPalUtil(num, dupNum); } // Driver Code 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"); } } // This code is contributed by mits |
PHP
<?php // A recursive PHP program to // check whether a given number // is palindrome or not // A function that reurns true // only if num contains one digit function oneDigit($num) { // comparison operation is faster // than division operation. So // using following instead of // "return num / 10 == 0;" return (($num >= 0) && ($num < 10)); } // A recursive function to find // out whether num is palindrome // or not. Initially, dupNum // contains address of a copy of num. function isPalUtil($num, $dupNum) { // Base case (needed for recursion // termination): This statement // mainly compares the first digit // with the last digit if (oneDigit($num)) return ($num == ($dupNum) % 10); // This is the key line in this // method. Note that all recursive // calls have a separate copy of // num, but they all share same // copy of *dupNum. We divide num // while moving up the recursion tree if (!isPalUtil((int)($num / 10), $dupNum)) return -1; // The following statements are // executed when we move up the // recursion call tree $dupNum = (int)($dupNum / 10); // At this point, if num%10 // contains i'th digit from // beiginning, then (*dupNum)%10 // contains i'th digit from end return ($num % 10 == ($dupNum) % 10); } // The main function that uses // recursive function isPalUtil() // to find out whether num is // palindrome or not function isPal($num) { // If num is negative, // make it positive if ($num < 0) $num = (-$num); // Create a separate copy of // num, so that modifications // made to address dupNum // don't change the input number. $dupNum = ($num); // *dupNum = num return isPalUtil($num, $dupNum); } // Driver Code $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"; // This code is contributed by m_kit ?> |
Output:
Yes No Yes No
To check a number is palindrome or not without using any extra space
This article is compiled by Aashish Barnwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Check if a number is Palindrome in PL/SQL
- Check if number is palindrome or not in Octal
- Program to check the number is Palindrome or not
- To check a number is palindrome or not without using any extra space
- Check if binary representation of a number is palindrome
- Recursive program to check if number is palindrome or not
- Generate number with given operation and check if it is palindrome
- Bash program to check if the Number is a Palindrome
- Check if a number with even number of digits is palindrome or not
- Check if actual binary representation of a number is palindrome
- Check if a string is palindrome in C using pointers
- Check if it is possible to create a palindrome string from given N
- Program to check if an Array is Palindrome or not
- C Program to Check if a Given String is Palindrome
- Check if any anagram of a string is palindrome or not



