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. That is, stdin = new Scanner(System.in); is given.

Respuesta :

Limosa

Answer:

//declare variable and initialize to 0

int odd=0;

//declare variable and initialize to 0

int oddCount=0;

//declare variable and initialize to 0

int evenCount=0;

//declare variables and initialize to 0 and 1

int even=0, i=1;

//iterates when i is greater than 0

while (i>0)

{

// get input in the variable i from the user

i = stdin.nextInt();

//check that i divided by 0, its remainder is 0 and i is greater than 0

if ((i % 2)==0 && (i>0))

{

//then, add the variable even with i

even+=i;

//and increase the count of the even by 1

evenCount++;

}

//check that i divided by 0, its remainder is not 0 and i is greater than 0

if ((i % 2)!=0 && (i>0))

{

//then, add the variable odd with i

odd+=i;

//and increase the count of the odd by 1

oddCount++;

}

}

//then, print the following message

System.out.print(even + " " + odd + " " + evenCount + " " + oddCount);

Explanation:

The following are the description of the program.

  • Firstly, declare the integer type variables 'odd', 'evenCount', 'oddCount', 'even' and initialize them to 0.
  • Then, set the while loop statement that iterates when the variable 'i' is greater than 0. Then, set the if conditional statement to check that the variable 'i' divided by 0, its remainder is 0 and the variable 'i' is greater than 0, then add the variable 'even' with 'i' and increase the count of the even by 1.
  • Again check that the variable 'i' divided by 0, its remainder is not 0 and the variable 'i' is greater than 0 then, add the variable 'odd' with the variable 'i' and increase the count of the odd by 1.
  • Finally, print the following message with the output.
ACCESS MORE