-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.c
39 lines (31 loc) · 800 Bytes
/
answer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdbool.h>
//-----------------------------------------------------------------------------
bool isPalindrome(int x){
int temp = x, digits = 1;
// If x is negative
if (x < 0)
return false;
// Finds 1^digits
while (temp >= 10){
temp /= 10;
digits *= 10;
}
while (x){
if ((x % 10) != (x / digits))
return false;
// Removes a digit off each side
x = (x % digits) / 10;
// Removes 2 digits
digits /= 100;
}
// If it makes it this far it is a palindrome
return true;
}
//-----------------------------------------------------------------------------
// Testing
int main(){
int test = 1000021;
printf("%d\n", isPalindrome(test));
return 0;
}