Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

1. ## 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..
```   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);

}
}```  Reply With Quote

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

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  Reply With Quote

4. ## 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!  Reply With Quote