0
votes

In this java program you (the user) keeps entering numbers until you enter a zero, which is when the list terminates. It will compute the sum of positive even & sum of odd even and negative numbers. It will also compute the average.

I'm stuck at the part to get the average of sum of even positive numbers, sum of odd positive numbers, and sum of negative numbers.

import java.util.*;
class sumPositiveAverage {
    public static void main (String[] args) {
        Scanner sc = new Scanner (System.in);
        System.out.println ("Enter numbers. List terminates when you enter a zero. 

        Enter a zero when you want to begin the addition.");
        int a = sc.nextInt();
        int esum=0;
        int osum=0;
        int nsum=0;
        while (a !=0)
        {
            if (a>0)
            {
                if (a%2==0)
                {
                    esum = esum+a;
                }// end of 3rd innermost if statement
                else
                {
                osum = osum+a;
                }// end of 3rd else statement
            }//end of 2nd middle if-else-loop
            else if (a<0)
            {
                nsum=nsum+a;
            }//end of 2nd middle else statement
        }//end of while loop
        System.out.println ("The sum of even positive numbers is "+esum);
        System.out.println ("The sum of odd positive numbers is "+osum);
        System.out.println ("The sum of negative numbers is "+nsum);
    }//end of main
}//end of class2
1
Add a counter for even and odd numbers and use those numbers to divide your even sum and odd sum after the loop.M.K.
Along with counter as suggested by @I.K., read the a at the end of the while loopHJK

1 Answers

0
votes
import java.util.*;

public class sumPositiveAverage {

public static void main(String[] args)
{
   int sum_even = 0;
   int sum_odd = 0;
   int sum_negative = 0;
   int sum_positive = 0;

   int count_even = 0;
   int count_odd = 0;
   int count_negative = 0;
   int count_positive = 0;

   double avg_sum_even = 0;
   double avg_sum_odd = 0;
   double avg_sum_negative = 0;
   double avg_sum_positive = 0;

   int sum = 0;
   int count = 0;
   Scanner input = new Scanner(System.in);
   int data = 0;
   do
   {
       System.out.print("Type in a positive or negative number and press enter key, if 0 is entered program stops: ");
       data = input.nextInt();


       if(data > 0){

           if(data%2 == 0){
           //even number
           sum_even = sum_even + data;
           count_even++;
           }else{

               //odd
               sum_odd = sum_odd + data;
               count_odd++;

           }

           sum_positive = sum_positive + data;
           count_positive++;
       }else if(data < 0) {

           //negative number
       sum_negative = sum_negative + data;
       count_negative++;

       }


    }
    //Stops if the value of data is ZERO(0) and continues if it's not
    while(data != 0);

   //here means zero has been entered

     if(count_positive > 0) { avg_sum_positive = (double)sum_positive/count_positive; }
     if(count_negative > 0) { avg_sum_negative = (double)sum_negative/count_negative; }
     if(count_even > 0) { avg_sum_even = (double)sum_even/count_even; }
     if(count_odd > 0) { avg_sum_odd = (double)sum_odd/count_odd; } 


     System.out.println("Sum of Positive Numbers = " + sum_positive);
     System.out.println("Sum of negative Numbers = " + sum_negative);
     System.out.println("Sum of odd Numbers = " + sum_odd);
     System.out.println("Sum of even Numbers = " + sum_even);

     System.out.println("Count of Positive Numbers = " + count_positive);
     System.out.println("Count of negative Numbers = " + count_negative);
     System.out.println("Count of odd Numbers = " + count_odd);
     System.out.println("Count of even Numbers = " + count_even);

     System.out.println("Average of Positive Numbers = " + avg_sum_positive);
     System.out.println("Average of negative Numbers = " + avg_sum_negative);
     System.out.println("Average of odd Numbers = " + avg_sum_odd);
     System.out.println("Average of even Numbers = " + avg_sum_even);


}//closing main
} // closing class