Skip to content

Instantly share code, notes, and snippets.

@brookskindle
Last active December 14, 2015 08:49
Show Gist options
  • Save brookskindle/5060982 to your computer and use it in GitHub Desktop.
Save brookskindle/5060982 to your computer and use it in GitHub Desktop.
Determine if an integer is a palindrome
#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