C++ Program For Palindrome Number Using WHILE Loop

A palindrome number is the number which is equal to its reverse.

For example, 161 is a palindrome number because if we reverse the order of digits then it is also the same number.

123 is not a palindrome number because when we reverse digits then 321 is produced which is not equal to original number 123.

Let’s break the process of finding palindrome number in steps:

  1. Get a number from user.
  2. Reverse the number.
  3. Compare reversed number with number entered by user.
  4. If both numbers are same, print “Palindrome.”.
  5. If both numbers are not equal, print “Not Palindrome.”.

I highly recommend you to read my tutorial about reversing a number which is the most crucial part of this program. Without reading that article, you might be unable to identify that how number is reversed.

C++ Code To Find Whether A Number is Palindrome or Not:

#include<iostream>
using namespace std;
int main()
{
	int num, original, remainder;
	int reverse=0;
	cout<<"ENTER A NUMBER : ";
	cin>>num;
	original=num;
	while(num!=0)
	{
		remainder=num%10;
		reverse=reverse*10+remainder;
		num=num/10;		
	}
	if(reverse==original)
	{
		cout<<"Yes, It is a Palindrome Number.";
	}
	else
	{
		cout<<"No, It is not a Palindrome Number.";
	}
	return 0;
}

C++ Code Explanation:

First of all, we declared four integer type variables: num, original, remainder & reverse.

  • num variable is used to store the number entered by user.
  • The value of num will be changed during program due to mathematical operation. So, we need another variable which stores the actual value thorougout the program. Thats why we declared original variable.
  • remainder variable will be used to store the remainder of division operation.
  • As we want to compare the reversed number with original number. So, variable reverse would be compared with original.

After taking input from user in num variable, we pass this variable to while loop which gives us the reverse of number.

 Recommended reading: Armstrong Number Program in C++ With Output

After that, we used IF condition and compared the reverse number with original number.

If both numbers are equal then program prints “Yes, It is a Palindrome Number.” .

If both numbers not equal then program shows “No, It is not a Palindrome Number.”.

Program Output:

C++ Program For Palindrome Number with output

When we entered 756, program showed us it is not a palindrome number which is correct.

Output of C++ Program for finding palindrome number

Dry Run:

Let’s see how a computer executes the instructions of this program. Suppose, the number entered by user is 696.

When num=696

remainder=696%10=6
reverse=0*10+6=0+6=6
num=696/10=69

When num=69

remainder=69%10=9
reverse=6*10+9=60+9=69
num=69/10=6

When num=6

remainder=6%10=6
reverse=69*10+6=690+6=696
num=6/10=0

If you think that this article is a bit useful then please share it with your friends.

If you have any question then you can ask me in comments. I would like to answer them.

You can also Click Here To Download Exercise Files.