fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <iomanip>
  4.  
  5. bool is_palindrome( int value )
  6. {
  7. std::vector<int> digits;
  8.  
  9. while( value != 0 )
  10. {
  11. digits.push_back( value % 10 );
  12. value /= 10;
  13. }
  14.  
  15. //instead of std::vector<int>::iterator... you can use auto
  16. std::vector<int>::const_iterator it_begin = digits.cbegin(); //you don't have to use const
  17. std::vector<int>::const_reverse_iterator it_end = digits.crbegin(); //you don't have to use const
  18.  
  19. while( it_begin != digits.cend() && it_end != digits.crend() )
  20. {
  21. if( *it_begin != *it_end ) return( false );
  22. ++it_begin;
  23. ++it_end;
  24. }
  25. return( true );
  26. }
  27.  
  28. int main()
  29. {
  30. std::cout << std::boolalpha;
  31. int input = 0;
  32. do
  33. {
  34. std::cout << "Please enter a number(-999 to quit): ";
  35. std::cin >> input;
  36. std::cout << "Is " << input << " a palindrome? " << is_palindrome( input ) << std::endl;
  37. } while( input != -999 );
  38.  
  39. return( 0 );
  40. }
Success #stdin #stdout 0s 3476KB
stdin
1
11
111
2
20
200
919
1231
-999
stdout
Please enter a number(-999 to quit): Is 1 a palindrome? true
Please enter a number(-999 to quit): Is 11 a palindrome? true
Please enter a number(-999 to quit): Is 111 a palindrome? true
Please enter a number(-999 to quit): Is 2 a palindrome? true
Please enter a number(-999 to quit): Is 20 a palindrome? false
Please enter a number(-999 to quit): Is 200 a palindrome? false
Please enter a number(-999 to quit): Is 919 a palindrome? true
Please enter a number(-999 to quit): Is 1231 a palindrome? false
Please enter a number(-999 to quit): Is -999 a palindrome? true