Answer:
import java.util.Scanner;
public class OddEven
{
public static void main(String[] args) {
int sumOfEven = 0, sumOfOdd = 0, countOfEven = 0, countOfOdd = 0;
Scanner stdin = new Scanner(System.in);
while(true) {
System.out.print("Enter a number: ");
int number = stdin.nextInt();
if (number > 0) {
if (number % 2 == 0) {
sumOfEven += number;
countOfEven++;
}
else if (number % 2 == 1) {
sumOfOdd += number;
countOfOdd++;
}
}
else
break;
}
System.out.println(sumOfEven +" "+sumOfOdd+" "+countOfEven+" "+countOfOdd);
}
}
Step-by-step explanation:
- Initialize the variables
- Create a while loop that iterates until the user enters 0 (Since the terminating condition is not really specified, I chose that)
Inside the loop:
- Ask the user to enter integers
- If the number is even, add it to the sumOfEven and increment countOfEven by 1
- If the number is odd, add it to the sumOfOdd and increment countOfOdd by 1
Then:
- Print out the values as requested