Finding the sum of even numbers using arrays

This is a discussion on Finding the sum of even numbers using arrays within the C Programming forums, part of the General Programming Boards category; I'm working on a problem for my programming class at the moment and cant figure out how to determine if ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    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. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    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;
    Last edited by Elysia; 12-03-2007 at 01:20 PM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Your compiler should warn you about that too.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Ah ok, Thanks that helped.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by Elysia View Post
    Code:
    sum=sum+num;
    Can also be written as:
    Code:
    sum += num;
    Just some clarification
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    Meh. Things doesn't seem to work out as they should these last days...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum of the numbers between two integers
    By Sridar in forum C++ Programming
    Replies: 16
    Last Post: 02-01-2011, 09:44 AM
  2. Finding Prime Numbers
    By dan724 in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 11:12 AM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 07:40 AM
  4. Replies: 4
    Last Post: 04-19-2005, 08:05 PM
  5. Array's Help
    By bmx4christ in forum C Programming
    Replies: 15
    Last Post: 12-08-2003, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21