stuck trying to count sum of even and odd numbers for reading loop.

so I've been working on this practice problem and im stuck. help would be much appreciated ;).

this is what the output should be.

12

30

60

31

90

11

15

Even numbers: 4

Even sum: 192

Odd numbers: 3

Odd sum: 57

and i have to read exactly 7 numbers from the user.

i cant figure out how to get the sum for the even and odd numbers.

this is what i have so far..

Code java:

import java.util.*;
public class practiceLoops {
public static void main ( String args[] )
{
Scanner scan = new Scanner (System.in);
int even = 0;
int sumE = 0;
int odd = 0;
int sumO = 0;
int g = 1;
int number = scan.nextInt();
while ( g <= 6)
{
if ( number % 2 == 0)
even++;
if (number % 2 == 1)
odd++;
g++;
number = scan.nextInt();
}
System.out.printf ("even number: %d \n" +
"Even sum: %d \n" +
"odd numbers %d \n" +
"Odd sum %d", even, sumE, odd, sumO);
}
}

Re: stuck trying to count sum of even and odd numbers for reading loop.

Quote:

how to get the sum for the even and odd numbers.

Add the number to the sum for that type of number:

If the number is even, add to sumOfEvens

if odd, add to sumOfOdds

Re: stuck trying to count sum of even and odd numbers for reading loop.

cool. i tried something like that before but it didn't work, now i get it.

thanks for the fast reply norm!