47.1k views
4 votes
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 single line and separated by single 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.

Additional Notes: Regarding your code's standard output, CodeLab will check for case errors and will check whitespace (tabs, spaces, newlines) exactly.

User Lsalamon
by
8.1k points

1 Answer

0 votes

Answer:

AI-generated answer

To solve this problem, we can use a loop that continuously reads positive integers from the standard input until it encounters a non-positive integer. Within the loop, we can keep track of the sum and count of even and odd integers separately.

Here is an example of how the loop can be implemented in Python:

```python

sum_even = 0 # variable to store the sum of even integers

sum_odd = 0 # variable to store the sum of odd integers

count_even = 0 # variable to store the count of even integers

count_odd = 0 # variable to store the count of odd integers

while True:

num = int(input("Enter a positive integer: ")) # read input as an integer

if num <= 0:

break # exit the loop if the input is non-positive

if num % 2 == 0:

sum_even += num # add the even integer to the sum_even

count_even += 1 # increment the count of even integers

else:

sum_odd += num # add the odd integer to the sum_odd

count_odd += 1 # increment the count of odd integers

print("Sum of even integers:", sum_even)

print("Sum of odd integers:", sum_odd)

print("Count of even integers:", count_even)

print("Count of odd integers:", count_odd)

```

In this code, we start by initializing four variables: `sum_even`, `sum_odd`, `count_even`, and `count_odd`. These variables will store the cumulative sum and count of even and odd integers, respectively.

Inside the while loop, we read an integer from the standard input using the `input()` function and convert it to an integer using the `int()` function. If the input is less than or equal to 0, we break out of the loop.

If the input is even (i.e., its remainder when divided by 2 is 0), we add it to the `sum_even` variable and increment the `count_even` variable. If the input is odd, we add it to the `sum_odd` variable and increment the `count_odd` variable.

After the loop terminates, we print the values of `sum_even`, `sum_odd`, `count_even`, and `count_odd` on a single line, separated by single spaces.

Let's say we input the following integers: 2, 5, 4, 7, -1. The program will compute the sum of even integers as 6 (2 + 4), the sum of odd integers as 12 (5 + 7), the count of even integers as 2, and the count of odd integers as 2. Therefore, the program will output:

```

Sum of even integers: 6

Sum of odd integers: 12

Count of even integers: 2

Count of odd integers: 2

```

Step-by-step explanation:

User Greta
by
8.1k points