# Thread: Finding the sum of even numbers using arrays

1. ## 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.

2. Code:
`if(A[i]&#37;2=0)`
You're assigning instead of comparing.
Code:
`sum=sum+num;`
Can also be written as:
Code:
`sum += num;`

3. 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.

4. Your compiler should warn you about that too.

5. Ah ok, Thanks that helped.

6. 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

7. Originally Posted by Elysia
Code:
`sum=sum+num;`
Can also be written as:
Code:
`sum += num;`
Just some clarification

8. Meh. Things doesn't seem to work out as they should these last days...

Popular pages Recent additions