codescracker
c++

C++ Program to Check Number is Equal to its Reverse



« Previous Program Next Program »


Here you will learn and get code on checking whether the input number is equal to its reverse or not in C++.

To check that the original number is equal to its reverse or not in C++ programming, you have to ask from user to enter the number first. Then initialize its value (number entered by user) to a variable say orig and then reverse that number (entered by user). Finally compares its reverse with orig to check whether reverse is equal to original or not as shown in the following program:

// C++ Program Given Number is equal to its Reverse or Not
// -----codescracker.com------

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

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

C++ program check reverse original

Now supply any input as input say 1234 to check whether its reverse is equal to the number itself or not as shown in the output given below:

reverse original program c

Here is another sample run with user input as 12321:

check number is equal to its reverse c++

Note - Make sure to initialize the input value to a variable say orig before reversing the number.

As you can see from above program, we have reversed the number and then compares it with original (value stored in orig).

Same Program in Other Languages


« Previous Program Next Program »