Advertisement
Donald_Fortier

bool isPalindrome( int );// C++

May 28th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. using namespace std;
  2.  
  3. bool isPalindrome( int );// Prototype for this function.
  4.  
  5. bool isPalindrome( int num ) {
  6.     int number, last, sum = 0;// Initialize some variables.
  7.     for ( number = num; num != 0; num /= 10 ) {// While integers are still in the sequence...
  8.         last = num % 10;// The last number in the sequence.
  9.         sum = sum * 10 + last;// Add the current last digit to a new sequence that reverses the original number.
  10.     }
  11.     if ( number != sum )// If the original number is not equal to the reversed number...
  12.         return false;// ... then it is not a palindrome since numbers don't match.
  13.  
  14.     return true;// The integer survived the tests and proved itself as a palindrome.
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement