C Program: Sum of Even Numbers and Product of Odd Numbers
C Program to Calculate Sum of Even and Product of Odd Numbers
C program that takes 10 numbers as input from the user and stores them in an array. It then calculates the sum of even numbers and the product of odd numbers in the array and displays the results:
Program 01: Even and Odd Number Operations in C: Step-by-Step Example
10 number through the keyword into an array and display the result of the addition of even numbers and product of odd numbers
#include<stdio.h>
int main() {
int arr[10];
int even_sum = 0;
int odd_product = 1;
// Read 10 numbers from the user and store them in the array
printf("Enter 10 numbers: ");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// Calculate sum of even numbers and product of odd numbers
for (int i = 0; i < 10; i++) {
if (arr[i] % 2 == 0) {
even_sum += arr[i];
} else {
odd_product *= arr[i];
}
}
// Display the results
printf("Sum of even numbers: %d\n", even_sum);
printf("Product of odd numbers: %d\n", odd_product);
return 0;
}
Enter 10 numbers: 2 4 6 1 3 5 7 8 9 10
Sum of even numbers: 30
Product of odd numbers: 945
In this program, we declare an integer array arr of size 10 to store the user input. We also declare two integer variables even_sum and odd_product to keep track of the sum of even numbers and the product of odd numbers, respectively.
We then use a for loop to read 10 numbers from the user and store them in the array. We use another for loop to iterate through the array and check each number to see if it is even or odd. If it is even, we add it to the even_sum variable. If it is odd, we multiply it to the odd_product variable.
Finally, we display the results by printing the values of even_sum and odd_product.