Add digits.c

Code for a friend
This commit is contained in:
Win 2024-05-19 18:25:22 +07:00
parent c33e0c90a4
commit 0ed9ce82e6
1 changed files with 50 additions and 0 deletions

50
digits.c Normal file
View File

@ -0,0 +1,50 @@
#include <stdio.h>
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;
}