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

Palindrome

C++ programs to check if a number is a palindrome or not have been shown here. For example, 1221 is a palindrome because we get the same number irrespective of the direction (left to right or right to left) of reading. But 1234 is not a palindrome as we get 4321 if we read it from right to left.

The algorithm, time complexity and pseudocode of the program have been shown below.






1. Algorithm to check if a number is a palindrome or not


1. Take a number n as input.

2. Copy value of n to another variable say m i.e. m = n.

3. Initialize a variable to 0 say reverse = 0.

4. Perform r = n % 10.

5. Perform reverse = reverse * 10 + r.

6. Perform n = n / 10

7. If n = 0 go to step 8 else go to step 3.

8. Check If reverse == m

9. If step 8 is true then n is palindrome else n is not palindrome.




2. Pseudocode to check if a number is a palindrome or not


Input : A number $n$

Output : $n$ is Palindrome or Not

1. Procedure checkPalindrome($n$):

2. $reverse \leftarrow 0$

3. $m \leftarrow n$

4. Repeat until $n \neq 0$

5. $reverse \leftarrow reverse * 10 + (n \mod 10)$

6. $n \leftarrow n / 10$

7. If $reverse == m$:

8. Return "Palindrome"

9. Else:

10. Return "Not Palindrome"

11. End Procedure





3. Time complexity to check if a number is a palindrome or not


Time Complexity: O(log(n))

Where n is the input number.





4. C++ Program & output to check if a number is a palindrome or not using iteration

Code has been copied
/***********************************
        alphabetacoder.com
C++ program to check if a number is 
a palindrome or not using iteration
************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int n, m, revnum = 0, r;

    // take input of the number
    cout << "Enter the number = ";
    cin >> n;

    // copy the number
    m = n;

    //find the reverse
    while (n != 0) {
        // extract the unit digit
        r = n % 10;
        // store the reverse number
        // give appropriate positional value
        // of each digit
        revnum = revnum * 10 + r;
        //divide the number by 10
        n = n / 10;
    }

    // check for palindrome
    if (m == revnum)
        cout << m << " is a palindrome!" << endl;
    else
        cout << m << " is not a palindrome!" << endl;

    return 0;
}

Output


Case 1:

Enter the number = 4567654

4567654 is a palindrome!


Case 2:

Enter the number = 123

123 is not a palindrome!





5. C++ Program & output to check if a number is a palindrome or not using recursion

Code has been copied
/***********************************
        alphabetacoder.com
C++ program to check if a number is 
a palindrome or not using iteration
************************************/

#include <iostream>

using namespace std;

// recursive function to check if
//  a number is a palindrome or not
int check_palindrome(int n, int num, int rev) {
    if (n == 0) {
        // reverse is same as original
        if (num == rev)
            return 1;
        else
            return 0;
    } else
        return check_palindrome(n / 10, num, (rev * 10 + n % 10));
}

int main() {
    // declare variables
    int num;

    // take input of the number
    cout << "Enter the number = ";
    cin >> num;
    
    // check for palindrome
    // by calling the function
    // If the function returns 1,
    // then the number is palindrome
    // else the number is not palindrome
    if (check_palindrome(num, num, 0))
        cout << num << " is a palindrome!" << endl;
    else
        cout << num << " is not a palindrome!" << endl;

    return 0;
}

Output


Case 1:

Enter the number = 90909

90909 is a palindrome!


Case 2:

Enter the number = 9090

9090 is not a palindrome!