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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: problem with odd/even

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problem with odd/even

    determining whats odd or even
    A couple of ways:
    Use the % operator with 2
    Test if the rightmost bit is a 1
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with odd/even

    sample code?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with odd/even

    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....

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problem with odd/even

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with odd/even

    i got the evens by i % 2 == 0. but i can't get the odd even with if else

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with odd/even

    Nvm i got the numbers now how would you add all values of odd or even

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problem with odd/even

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with odd/even

    i never used counters before can you explain

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with odd/even

    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

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problem with odd/even

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with odd/even

    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

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problem with odd/even

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with odd/even

    true but i got a 100

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problem with odd/even

    i got a 100
    ?? No accounting for the grader.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Odd and Even Numbers
    By tyb97 in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 30th, 2012, 04:19 PM
  2. WolfMud odd problem
    By wilikas in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 10th, 2011, 06:21 AM
  3. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  4. Odd/Even
    By SOK in forum What's Wrong With My Code?
    Replies: 16
    Last Post: September 27th, 2010, 07:47 AM
  5. Odd Even Zero Counter?
    By velop in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 02:13 PM