From 0ed9ce82e61fc8b7c15552bb0683b0329ff30783 Mon Sep 17 00:00:00 2001 From: Win Date: Sun, 19 May 2024 18:25:22 +0700 Subject: [PATCH] Add digits.c Code for a friend --- digits.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 digits.c diff --git a/digits.c b/digits.c new file mode 100644 index 0000000..61a7bf0 --- /dev/null +++ b/digits.c @@ -0,0 +1,50 @@ +#include + +int main() { + + // declaring and defining the 14 digit numbers + int digits[] = {3, 4, 2, 1, 5, 6, 8, 3, 1, 5, 1, 5, 6, 5}; + + // declaring the variable for the total of the first, third, fifth elements and so on... + int sum_odd; + + printf("=====================================\n"); + printf("First set of digits\n"); + printf("=====================================\n"); + // loop through the first, third, fifth element and so on + for(size_t i = 0; i < (sizeof(digits) / sizeof(digits[0])); i += 2) { + // multiplying the value of each element by 2 + digits[i] = digits[i] * 2; + printf("Element %d: %d\n", ((int)i + 1), digits[i]); + + // adding the value of the current element to the old sum_even value + sum_odd += digits[i]; + } + + // declaring the variable for second, forth, sixth elements and so on... + int sum_even; + + printf("\n"); + printf("=====================================\n"); + printf("Second set of digits\n"); + printf("=====================================\n"); + // loop through the second, forth, sixth digits and so on + for(size_t j = 1; j < (sizeof(digits) / sizeof(digits[0])); j += 2) { + // multiplying the value of each element by 2 + digits[j] = digits[j] * 2; + printf("Element %d: %d\n", ((int)j + 1), digits[j]); + + // adding the value of the current element to the old sum_even value + sum_even += digits[j]; + } + + // add the sum of sum_odd and sum_even together + int total = sum_odd + sum_even; + // print out the value + printf("\n"); + printf("=====================================\n"); + printf("Total: %d\n", total); + printf("=====================================\n"); + + return 0; +}