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 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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); 
     
     
          }
       }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

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

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. Recurring(Looping) Sum, Count and Average
    By apagnihotri in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 13th, 2012, 08:37 AM
  2. Sum of even and odd integers
    By spikezone2004 in forum Object Oriented Programming
    Replies: 10
    Last Post: February 8th, 2012, 06:03 PM
  3. sum of arrays and printing even and odd numbers in the array
    By senecawolf in forum Collections and Generics
    Replies: 3
    Last Post: November 8th, 2011, 03:07 PM
  4. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 03:30 PM
  5. Odd and Even Numbers Logics in FOR and IF Loop methods
    By mparthiban in forum Loops & Control Statements
    Replies: 2
    Last Post: May 12th, 2010, 06:19 AM