C++ 回文程序
回文数是在反向后相同的数字。例如121、34543、343、131、48984是回文数。
回文数算法
从用户那里获取电话号码
将数字保留在临时变量中
倒数
比较临时号码和反向号码
如果两个数字相同,则打印回文编号
其他打印而非回文编号
让我们看看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.