Thread: Finding the sum of even numbers using arrays

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 02:20 PM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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
    431
    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
    Chappell Hill, Texas
    Posts
    2,332
    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,817
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 10:44 AM
  2. Finding Prime Numbers
    By dan724 in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 12:12 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08: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, 12:40 PM