upd
This commit is contained in:
26
9. Palindrome Number/solution.c
Normal file
26
9. Palindrome Number/solution.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool isPalindrome(int x) {
|
||||
// negatives and numbers ending in 0 (except 0 itself) are not palindromes
|
||||
if (x < 0 || (x % 10 == 0 && x != 0))
|
||||
return false;
|
||||
|
||||
int reversedHalf = 0;
|
||||
|
||||
while (x > reversedHalf) {
|
||||
reversedHalf = reversedHalf * 10 + x % 10;
|
||||
x /= 10;
|
||||
}
|
||||
|
||||
// for even digits: x == reversedHalf
|
||||
// for odd digits: x == reversedHalf / 10
|
||||
return (x == reversedHalf) || (x == reversedHalf / 10);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user