1

I need to write code which would get out 6 palindrome numbers on screen.

Example: 300003 310013 320023 330033 340043 350053.

Findings: So far I have just written code how to check if its palindrome number or not.

Here is code for how i check if its palindrome or not:

#include <iostream>
using namespace std;

int main()
{
     int n, num, dig, rev = 0;

     cout << "Insert number": "<< endl;
     cin >> num;

     n = num;


   while (num != 0);
     {
         dig = num % 10;
         rev = (rev * 10) + dig;
         num = num / 10;
     } 

     if (n == rev)
         cout << "This is palindrome "<< rev << endl;
     else
         cout << "This is not palindrome "<< rev << endl;

    return 0;
}

Can you guys give some ideas how I can do that?

DevDio
  • 1,525
  • 1
  • 18
  • 26
Rinalds
  • 11
  • 1
  • 4

3 Answers3

2

You are stuck in an infinite while loop int the line:

while (num != 0);

So remove the semicolon and it will work fine.

Raindrop7
  • 3,889
  • 3
  • 16
  • 27
0

One thing you could do is convert the number that was given into a string via std::to_string.

Then use std::reverse to reverse the string, and compare to the original.

0

What about this?

#include <iostream>
using namespace std;

bool is_palindrom(int num)
{
     int dig,rev=0,n=num;
     while (num != 0)
     {
         dig = num % 10;
         rev = (rev * 10) + dig;
         num = num / 10;
     }

     return (n == rev);
}
int main()
{
     vector<int> v={300003, 310013, 320023, 330033, 340043, 350053,123};
     for(auto n:v)
     {
         cout<<n<<" is "<<(is_palindrom(n)?"":"not ")<<"palindrom."<<endl;
     }
}