codescracker
c++

C++ Program to Check Palindrome Number



« Previous Program Next Program »

In this article, you will learn and get code to check whether a number entered by user (at run-time) is a palindrome number or not in C++. Here are the list of programs on checking palindrome number:

What is a Palindrome Number ?

If reverse of a number is equal to the number itself, then it is called as a palindrome number. For example, 121, 15751 etc.

Check Palindrome Number or Not

To check whether a number given by user is a palindrome number or not in C++, first receive the number, initialize its value to a variable say temp and reverse the value of temp. After reversing, check whether the original number is equal to temp or not.

That is, if original number is equal to the value of rev (original number's reverse or temp's reverse), then print a message like, it is a palindrome number, otherwise print a message like, it is not a palindrome number as shown in the program given below:

// C++ Program to Check Palindrome Number or Not
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int num, rev=0, rem, temp;
    cout<<"Enter the Number: ";
    cin>>num;
    temp = num;
    while(temp>0)
    {
        rem = temp%10;
        rev = (rev*10)+rem;
        temp = temp/10;
    }
    if(rev==num)
        cout<<"\nIt is a Palindrome Number";
    else
        cout<<"\nIt is not a Palindrome Number";
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program check palindrome number

Now supply the input say 12321 and press ENTER key to check and print whether it is a palindrome number or not as shown in the snapshot given below:

check palindrome number in c

Here is another sample run with user input, 12345:

c++ check palindrome number

In above program, we have initialized the original number stored in num to temp. Then the value of temp gets reversed. Finally compared with the reversed value (value of rev) to the original number (value of num). If it is found to be equal, then print as a palindrome number, otherwise print as not a palindrome number.

To learn about reversing of a number, refer to C++ Program to Reverse a Number article to get every required things about the topic.

Check Palindrome Number using Function

The question is, write a program in C++ that checks for palindrome number or not using user-defined function. Here is its answer:

// C++ Check Palindrome Number using Function
// ----codescracker.com----

#include<iostream>
using namespace std;
int checkPalindrome(int);
int main()
{
    int num, val;
    cout<<"Enter the Number: ";
    cin>>num;
    val = checkPalindrome(num);
    if(val==0)
        cout<<"\nIt is a Palindrome Number";
    else
        cout<<"\nIt is not a Palindrome Number";
    cout<<endl;
    return 0;
}
int checkPalindrome(int n)
{
    int temp, rem, rev=0;
    temp = n;
    while(temp>0)
    {
        rem = temp%10;
        rev = (rev*10)+rem;
        temp = temp/10;
    }
    if(rev==n)
        return 0;
    else
        return 1;
}

Here is its sample run with user input, 55:

check palindrome number using function c++

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker