Skip to content

Commit

Permalink
Palindrome number
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Sep 20, 2017
1 parent 995b5e2 commit b1393ac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 009_palindrome_number/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test palindrome_number.c
34 changes: 34 additions & 0 deletions 009_palindrome_number/palindrome_number.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

static int reverse(int x)
{
int y = 0;
while (x) {
y = y * 10 + x % 10;
x /= 10;
}
return y;
}

static bool isPalindrome(int x) {
if (x == 0) {
return true;
}
if (x < 0) {
return false;
}
return x == reverse(x);
}

int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "Usage: ./test number\n");
exit(-1);
}

printf("%s\n", isPalindrome(atoi(argv[1])) ? "true" : "false");
return 0;
}

0 comments on commit b1393ac

Please sign in to comment.