From 3b71bbbca8b4aecc10fb1b3e26639bf31f010a42 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Sat, 19 Nov 2022 17:43:23 +0500 Subject: [PATCH] Problem 9: palindrome number --- problems/9-palindrome-number/Makefile | 1 + problems/9-palindrome-number/lib.c | 32 +++++++++++++++++++++++++++ problems/9-palindrome-number/lib.h | 4 ++++ problems/9-palindrome-number/main.c | 18 +++++++++++++++ 4 files changed, 55 insertions(+) create mode 120000 problems/9-palindrome-number/Makefile create mode 100644 problems/9-palindrome-number/lib.c create mode 100644 problems/9-palindrome-number/lib.h create mode 100644 problems/9-palindrome-number/main.c diff --git a/problems/9-palindrome-number/Makefile b/problems/9-palindrome-number/Makefile new file mode 120000 index 0000000..25eaf27 --- /dev/null +++ b/problems/9-palindrome-number/Makefile @@ -0,0 +1 @@ +../template/Makefile \ No newline at end of file diff --git a/problems/9-palindrome-number/lib.c b/problems/9-palindrome-number/lib.c new file mode 100644 index 0000000..7fea0bd --- /dev/null +++ b/problems/9-palindrome-number/lib.c @@ -0,0 +1,32 @@ +#include "lib.h" + +bool isPalindrome(int x) { + if (x < 0) + return false; + + // Find length and calculate divisor + int len = 0; + int div = 1; + int copy = x; + while (copy > 0) { + copy /= 10; + if (len != 0) + div *= 10; + len++; + } + + // Compare digits + for (int i=0; i + +bool isPalindrome(int x); diff --git a/problems/9-palindrome-number/main.c b/problems/9-palindrome-number/main.c new file mode 100644 index 0000000..37d3532 --- /dev/null +++ b/problems/9-palindrome-number/main.c @@ -0,0 +1,18 @@ +#include +#include "lib.h" + +int main() { + int a = 121; + printf("Test 1: %d\n", isPalindrome(a) == 1); + + int b = -121; + printf("Test 2: %d\n", isPalindrome(b) == 0); + + int c = 1234554321; + printf("Test 3: %d\n", isPalindrome(c) == 1); + + int d = 2147483647; + printf("Test 4: %d\n", isPalindrome(d) == 0); + + return 0; +}