回文数字是一种反向后也相同的数字(从左边读与从右边读都是同一个数字)。 例如:121
,34543
,343
,131
,4894
这些都是回文数。
回文数算法
下面来看看看c++中如何实现回文的一个程序。 在这个程序中,将从用户得到一个输入,并检查数是否是回文。
#include <iostream>
using namespace std;
int main()
{
int n,r,sum=0,temp;
cout<<"enter the number=";
cin>>n;
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
cout<<"number is palindrome.";
else
cout<<"number is not palindrome.";
return 0;
}
输出结果 -
enter the number=121
number is palindrome.
enter the number=113
number is not palindrome.