Palindrome C ++程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Palindrome C ++程序相关的知识,希望对你有一定的参考价值。

我希望有人帮助我纠正我在这个程序中的错误。

#include<iostream.h>
#include<conio.h>
void main();
{int r,n,Rev=0,temp;

    cin>>n;
    temp=n;
    while(n>0)
        Rev=Rev*10+n%10;
    n=n/10;
    if(temp==Rev)
        cout<<"test is positive";
    else
        cout<<"test is negative";
    getch();
}

Rev表示我们在反转数字时获得的数字。在阳性测试的情况下,它变成了回文,否则它不会.Temp是临时变量

答案

这条线在while之外:

  n/n10;

所以n永远不会<0。

    #include<iostream.h>
    #include<conio.h>

        void main();
        {
int r,n,Rev=0,temp;

            cin>>n;
            temp=n;
            while(n>0){
                Rev=Rev*10+n%10;
            n=n/10;
            }

            if(temp==Rev)
                cout<<"test is positive";
            else
                cout<<"test is negative";
            getch();
        }
另一答案
#include<iostream>
    using namespace std;
        int main()
        {
            int num,rev,pal;
            cout<<"Enter number ";cin>>num;
            pal=num;
            for( ;num!=0; )
            {
                rev=(0*10)+num%10;
                num/=10;
            }
            if(pal==rev)
                cout<<"number id palindrome";
            else
                cout<<"Number is not palindrome";

以上是关于Palindrome C ++程序的主要内容,如果未能解决你的问题,请参考以下文章