cpp program to check palindrome or not
This program tells about the number is the palindrome or not by using while loop.Reverse the number and match with the original number .
For understanding you should learn the while loop first.
code
#include <iostream>
using namespace std;
int main()
{
int originalnumber, num, digit, reversenumber = 0;
cout << "Enter a positive number: ";
cin >> num;
originalnumber = num;
while (num != 0)
{
digit = num % 10;
reversenumber = (reversenumber * 10) + digit;
num = num / 10;
}
cout << " The reverse of the number is: " <<reversenumber<< endl;
if (originalnumber == reversenumber)
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";
return 0;
}
Here,the while loop is used to separate the all the digits of the number and add again in the reverse manner for reversing the number.In this process,modulus the num (num%10) separate the one digit ,
(reversenumber*10)+digit store in the reverse number,and ( num = num / 10;) divide the number by 10 repeat the loop till the number is equal to or less than the zero.
In the last,match with original number tell about the number is palindrome or not.
If you any problem with this program then openly share your query in the comment section.
Comments
Post a Comment