Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out, on a line by itself and separated by spaces, the sum of all the even integers read, the sum of all the odd integers read, a count of the number of even integers read, and a count of the number of odd integers read, all separated by at least one space. Declare any variables that are needed. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

Respuesta :

ijeggs

Answer:

import java.util.Scanner;

public class num15 {

   public static void main(String[] args) {

   Scanner in = new Scanner(System.in);

       int sumEven =0;

       int sumOdds =0;

       int countEven =0;

       int countOdd =0;

       System.out.println("Enter numbers");

       int num = in.nextInt();

       if(num%2==0){

           sumEven = num;

           countEven++;

       }

       else{

           sumOdds =num;

           countOdd++;

       }

    while(num>0){

        System.out.println("Enter another number: ");

        num = in.nextInt();

        if(num%2==0){

            sumEven= sumEven+num;

            countEven++;

        }

        else{

            sumOdds = sumOdds+num;

            countOdd++;

        }

   }

       System.out.println("Sum of Odds "+sumOdds);

       System.out.println("Sum of Evens "+sumEven);

       System.out.println("Count Odds "+countOdd);

       System.out.println("Count Evens "+(countEven-1));

   }

}

Explanation:

The complete program in Java is given above

Using the scanner class the user is repetitively prompted to enter a number

The while(num>0) loop ensures that only positive numbers greater than 0 are entered, when 0 or a negative number is entered the loop terminates.

Within the while loop an if/else statement is used to keep track of the sumEvens, countEvens, sumOdds and countOdds

The values of these variables are printed on separate line

See code output attached.

Ver imagen ijeggs