Open In App

C++ Program to Check Whether a Number is Palindrome or Not

Last Updated : 22 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 <iostream>
using namespace std;
 
// A function that returns 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
    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 beginning, 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)
{
     
    // Check 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);
 
    return isPalUtil(num, dupNum);
}
 
// Driver code
int main()
{
    int n = 12321;
    isPal(n) ? cout << "Yes"
               cout << "No" << endl;
 
    n = 12;
    isPal(n) ? cout << "Yes":
               cout << "No" << endl;
 
    n = 88;
    isPal(n) ? cout << "Yes":
               cout << "No" << endl;
 
    n = 8999;
    isPal(n) ? cout << "Yes":
               cout << "No";
    return 0;
}
 
// This code is contributed by shivanisinghss2110


Output: 

Yes
No
Yes
No 

Time complexity: O(log(n))
Auxiliary space: O(1).

To check a number is palindrome or not without using any extra space
Method #2:Using string() method

  1. 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.
  2. 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




// C++ implementation of the
// above approach
#include <iostream>
using namespace std;
 
// Function to check palindrome
int checkPalindrome(string str)
{
    // Calculating string length
    int len = str.length();
   
    // Traversing through the string
    // upto half its length
    for (int i = 0; i < len / 2; i++)
    {     
        // Comparing i th character from
        // starting and len-i th character
        // from end
        if (str[i] != str[len - i - 1])
            return false;
    }
   
    // If the above loop doesn't return
    // then it is palindrome
    return true;
}
 
// Driver Code
int main()
{
    // Taking number as string
    string st =
    "112233445566778899000000998877665544332211";
    if (checkPalindrome(st) == true)
        cout << "Yes";
    else
        cout << "No";
    return 0;
}
// This code is written by vikkycirus


Output:

Yes

Time Complexity: O(|str|)

Space Complexity: O(1) as no extra space has been used. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads