fork(3) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // checks whether the given integer is a palindrome
  7.  
  8. int is_palindrome(int n)
  9. {
  10. int i;
  11. int nrev = 0;
  12.  
  13. // create a reversed version of n by adding up the
  14. // digits from the right into a new number, one by
  15. // one (most significant digit becomes least significant)
  16.  
  17. for (i=n; i>0; i=i/10)
  18. nrev = (10*nrev) + (i%10);
  19.  
  20. // palindrome has nrev the same as n
  21.  
  22. if (nrev==n)
  23. return 1;
  24. else
  25. return 0;
  26. }
  27.  
  28. // main function
  29.  
  30. int main()
  31. {
  32. int n;
  33.  
  34. // read integer n from input
  35.  
  36. cin >> n;
  37.  
  38. // check whether the integer is a palindrome, and print out
  39. // the result
  40.  
  41. if ( is_palindrome(n) )
  42. cout << n << " is a palindrome." << endl;
  43. else
  44. cout << n << " is not a palindrome." << endl;
  45.  
  46. // return 0 from main
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0.02s 2728KB
stdin
101
stdout
101 is a palindrome.