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
Re: problem with odd/even
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.
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....
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?
Re: problem with odd/even
i got the evens by i % 2 == 0. but i can't get the odd even with if else
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.
Re: problem with odd/even
Nvm i got the numbers now how would you add all values of odd or even
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.
Re: problem with odd/even
i never used counters before can you explain
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.
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
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?
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
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.
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
Re: problem with odd/even
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.
Re: problem with odd/even
Re: problem with odd/even
?? No accounting for the grader.