0
/*
  david ballantyne
  10/10/13
  assesment lab 2
*/

//Libraries
#include <iostream>


//Global Constants

//Functioning Prototypes
using namespace std;

int main() {
         int n, num, digit, rev = 0;
         cout << "Enter a positive number"<<endl;
         cin >> num;
         num=n;

         do{
             digit = num%10;
             rev = (rev*10) + digit;
             num = num/10;

}
    while (num!=0);
         if (n == rev)
         cout << " The number is a palindrome"<<endl;
        else
         cout << " The number is not a palindrome"<<endl;
return 0;
}

I enter a palindrome and it keeps telling me it's not a palindrome, also if you could help me understand what kind of loop I would use to ask a "would you like to try again y/n" I'd be grateful.

4

3 回答 3

2

将数字转换为字符串可能更容易。创建一个与第一个字符串相反顺序的新字符串。然后比较一下是否相同。真的没有理由做任何真正的数学,回文是词汇,而不是数学。

于 2013-10-10T17:01:36.413 回答
1
cin >> num;
num=n;

分配一个用户指定的整数,num然后用未初始化变量的值替换它n

你的意思是撤销分配吗

n=num;

反而?

另外,如果你能帮助我理解我会用什么样的循环来问“你想再试一次吗?”

在您报告数字是否为回文后,您可以使用do...while带有计算条件的循环。while

于 2013-10-10T16:59:28.107 回答
-2
#include <iostream>
using namespace std;

 int main()
{
  int userNum, palindrome[100], rem, rem2, count=0, count2=0, compare,  
   compare2;
  bool flag;

cout << "Enter number to test for Palindrome: ";
cin >> userNum;

compare = userNum;
compare2 = userNum;

// counting the digits in the number.
do {
    rem = compare % 10;
    count += 1;
    compare /= 10;
} while (compare >= 1);

// inputing in an array.
for (int i=0; i<count; i++)
{
    rem2 = compare2 % 10;
    palindrome[i] = rem2;
    compare2 /=10;
}

// Comparing array with palindrome.
for (int i=0; i < count; i++)
{
    if (palindrome[i] != palindrome[count-i-1])
        count2 = 1;
}

if (count2 == 1)
    cout << "Not a palindrome.";
else
    cout << "Palindrome\n";

return 0;




    }
于 2018-04-04T13:58:15.450 回答