Give the c code for a for loop that sums the even values of
Give the c code for a for loop that sums the even values of an integer array that is MAX_LEN long. Your c code should include a print statement to print the final sum. For example, if the array contained 4,5,2,6,8,3 the sum would be 4+2+6+8 = 20. Do not submit the entire c file, just submit the code for the loop and the printing.
Solution
Prpgram:
#include <stdio.h>
int main()
{
int MAX_LEN[100];
int i, n, even_sum=0;
printf(\"Enter size of the array: \");
scanf(\"%d\", &n);
printf(\"Enter %d elements in the array: \", n);
for(i=0; i<n; i++)
{
scanf(\"%d\", &MAX_LEN[i]);
}
for(i=0; i<n; i++)
{
if (MAX_LEN[i] % 2 == 0)
even_sum+=MAX_LEN[i];
}
printf(\"Sum of all even numbers in the array is = %d\ \", even_sum);
return 0;
}
Output:
Enter size of the array:5
Enter 5 elements in the array:
3 4 6 11 12
Sum of all even numbers in the array is = 22
