# Finding the sum of even numbers using arrays

• 12-03-2007
Fox101
Finding the sum of even numbers using arrays
I'm working on a problem for my programming class at the moment and cant figure out how to determine if the number is even before including it in the sum. This is what I have so far, my compiler says there is an error with the if statement but I am not sure what else to do.

Code:

```void sum_even(void) {         int A[10], i, num, sum=0;                 for(i=0;i<10;i++);         {                 printf("Enter the value for A[%d]: ",i);                 scanf("%d", &A[i]);                                 if(A[i]%2=0)                 num=A[i];                 sum=sum+num;         }                 printf("The sum of the even numbers is %d.", sum);                         }```
Any help would be appreciated. The goal is to find the sum of the even numbers.
• 12-03-2007
Elysia
Code:

`if(A[i]&#37;2=0)`
Code:

`sum=sum+num;`
Can also be written as:
Code:

`sum += num;`
• 12-03-2007
MacGyver
Code:

`if(A[i]&#37;2=0)`
In C, = means assignment and == checks if two expressions are equal.

Code:

```if(A[i]%2=0) num=A[i]; sum=sum+num;```
An if an if test is true, then only one statement is executed unless you group multiple statements within braces.
• 12-03-2007
NeonBlack
• 12-03-2007
Fox101
Ah ok, Thanks that helped.
• 12-03-2007
Dino
If you only want to add to the sum when the number is even, you need to make sure the sum=sum+num is under the scope of the if statement.

Todd
• 12-03-2007
hk_mp5kpdw
Quote:

Originally Posted by Elysia
Code:

`sum=sum+num;`
Can also be written as:
Code:

`sum += num;`

Just some clarification ;)
• 12-03-2007
Elysia
Meh. Things doesn't seem to work out as they should these last days...