From c4981436af5e55ce8c944a55a0cc81958ef08e40 Mon Sep 17 00:00:00 2001 From: juan Date: Mon, 25 Oct 2021 20:25:25 +0800 Subject: [PATCH] add 1 - 2 --- pset5/1-numbers.c | 25 +++++++++++++++++++++++++ pset5/2-swap.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 pset5/1-numbers.c create mode 100644 pset5/2-swap.c diff --git a/pset5/1-numbers.c b/pset5/1-numbers.c new file mode 100644 index 0000000..127eb5d --- /dev/null +++ b/pset5/1-numbers.c @@ -0,0 +1,25 @@ +#include + +int main(void) { + // To store answer + int nums[10]; + long long n; + int i = 0; + + scanf("%lld", &n); + while (n > 0) { + nums[i] = n % 10; + n /= 10; + i++; + } + + for (int j = i - 1; j >= 0; j--) { + if (j == 0) { + printf("%i\n", nums[j]); + } else { + printf("%i ", nums[j]); + } + } + + return 0; +} diff --git a/pset5/2-swap.c b/pset5/2-swap.c new file mode 100644 index 0000000..685f98f --- /dev/null +++ b/pset5/2-swap.c @@ -0,0 +1,32 @@ +#include + +// Function prototypes +void swap(int *a, int *b); + +int main(void) { + // Initialize variables + int n; + int length = 0; + int nums[10]; + + // Get input + scanf("%d", &n); + + // Tabulate nums[] + while (n > 0) { + nums[length] = n % 10; + n /= 10; + length++; + } + + // See if anything is wrong + if (nums[0] == 0) { + printf("The number cannot be changed.\n"); + } else { // Print answer + for (int i = 0; i < length; i++) { + printf("%i", nums[i]); + } + printf("\n"); + } + return 0; +}