Last active
December 14, 2015 08:49
-
-
Save brookskindle/5060982 to your computer and use it in GitHub Desktop.
Determine if an integer is a palindrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
bool isPal(int num); | |
bool isPal(int num) | |
{ | |
vector<int> val; | |
//stick each digit from num into a vector | |
while(num > 0) | |
{ | |
val.push_back(num % 10); | |
num /= 10; | |
} | |
//traverse digits | |
for(int i = 0, j = val.size() - 1; i < j; ++i, --j) | |
{ | |
//numbers don't match | |
if(val[i] != val[j]) | |
return false; //not a palindrome | |
} | |
return true; //is a palindrome | |
} | |
int main(void) | |
{ | |
int num = 0; | |
cout << "Enter an integer: "; | |
cin >> num; | |
cout << num << " is" << (isPal(num) ? " " : " not ") << "a palindrome" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment