# problem with odd/even

• December 30th, 2012, 06:53 PM
silverpen10
problem with odd/even
this is the question i was given "Write a program that loops through a series of integers in the range 0 to 25, determines whether each integer is even or odd, and displays the sum of the even integers and the sum of the odd integers in that range. You must use a single loop for this program.

The output should be formatted as follows:

The sum of the even integers is ___
The sum of the odd integers is ___ "

this is how far i got in code

Code Java:

```public class Unit6_1 // Peter Krasinski { public static void main( String [] args ) { int i; int odd; int even; for (i = 1; i <= 25 ; i++) {   } } }```
I'm having a problem determining whats odd or even
• December 30th, 2012, 07:02 PM
Norm
Re: problem with odd/even
Quote:

determining whats odd or even
A couple of ways:
Use the % operator with 2
Test if the rightmost bit is a 1
• December 30th, 2012, 07:57 PM
silverpen10
Re: problem with odd/even
sample code?
• December 30th, 2012, 08:07 PM
Norm
Re: problem with odd/even
What have you tried? % is an arithmetic operator used the same as + or *
Write a small test program that uses the % operator and print out the results to see what it does.
• December 30th, 2012, 08:36 PM
silverpen10
Re: problem with odd/even
Code Java:

```public class Unit6_1 // Peter Krasinski { public static void main( String [] args ) { int i = 1; int odd; int even; while ( i <= 25 ) {   System.out.println(i%2); i++; } } }```

i get 101010101....
• December 30th, 2012, 08:41 PM
Norm
Re: problem with odd/even
Quote:

i get 101010101...
That's strange, I'd expect you'd get this with the posted code:
1
0
1
0
1
0
...
Add some more to the println so it also prints out the value of i as well as the value of i%2 so you can easily see the results.

What is the results of i%2 for even numbers?
What is it for odd numbers?
• December 30th, 2012, 11:13 PM
silverpen10
Re: problem with odd/even
i got the evens by i % 2 == 0. but i can't get the odd even with if else
• December 31st, 2012, 05:33 AM
Norm
Re: problem with odd/even
Please post the code you are having problems with.
If you can detect an even number then the else part must be odd. A number is one or the other.
• December 31st, 2012, 08:36 AM
silverpen10
Re: problem with odd/even
Nvm i got the numbers now how would you add all values of odd or even
• December 31st, 2012, 08:41 AM
Norm
Re: problem with odd/even
Quote:

how would you add all values
Use two counters, one for even, one for odd.
Use an if statement to determine if the number is odd or even
Add an odd number to the odd counter
add an even number to the even counter.
• December 31st, 2012, 08:50 AM
silverpen10
Re: problem with odd/even
i never used counters before can you explain
• December 31st, 2012, 09:08 AM
Norm
Re: problem with odd/even
Counter is a generic term I use for a variable (usually an int) that holds a count of something.

I guess counter is the wrong term here. The code is supposed to total some numeric values not count them.
• December 31st, 2012, 09:09 AM
silverpen10
Re: problem with odd/even
Code Java:

```public class Unit6_1 // Peter Krasinski { public static void main( String [] args ) { int i = 0; int sumODD = 0, sumEVEN = 0; while ( ++i <= 25 ) { if (i % 2 == 0) { sumEVEN += i; System.out.println(sumEVEN); } else { sumODD += i; System.out.println(sumODD); } } } }```
is this kinda what you mean
but how do i pull the final integer
• December 31st, 2012, 09:18 AM
Norm
Re: problem with odd/even
Quote:

how do i pull the final integer
Can you explain?
What does "pull" mean?
What is the final integer?

Why are you using a while loop instead of a for loop?
• December 31st, 2012, 09:24 AM
silverpen10
Re: problem with odd/even
every time it loops it displays the sum of the next even or odd number. the final is the sum of all numbers. i want to display that number alone
• December 31st, 2012, 09:53 AM
Norm
Re: problem with odd/even
Look at where the println statements are located. They are INSIDE the loop. They are executed every time the loop goes around. If you want them to execute at a different time, move them so they are executed when you want them to execute.
• December 31st, 2012, 10:59 AM
silverpen10
Re: problem with odd/even
Code java:

```public class Unit6_1 // Peter Krasinski { public static void main( String [] args ) { int i = 0; int sumODD = 0, sumEVEN = 0; while ( ++i <= 25 ) { if (i % 2 == 0) { sumEVEN += i; } else { sumODD += i; } } System.out.println("The sum of the odd integers is " + sumODD); System.out.println("The sum of the even integers is " + sumEVEN); } }```
is this good. it works
• December 31st, 2012, 11:04 AM
Norm
Re: problem with odd/even
Quote:

it works
Why are you using a while loop instead of a for loop?
for loops are for when you use an index that starts at one value and changes that index until it has another value.
While loops use conditions that can be changed by events or computations to control the loop.
• December 31st, 2012, 01:26 PM
silverpen10
Re: problem with odd/even
true but i got a 100
• December 31st, 2012, 01:30 PM
Norm
Re: problem with odd/even
Quote:

i got a 100
?? No accounting for the grader.