6.5k views
3 votes
Open) Write a program that reads a sequence of positive integer inputs until the user enters -1. The output should display the following results. NOTE: -1 Should NEVER be considered in the calculations. • The smallest and largest of all the inputs. • The number of even values, and the number of odd values.

1 Answer

1 vote

Final answer:

The question is about writing a program that reads positive integers until -1 is entered, then displays the smallest and largest values and counts of even and odd numbers, excluding the -1. A Python code snippet is provided that initializes variables, prompts for input, updates the variables, and displays the results after input ends.

Step-by-step explanation:

The subject of the question involves writing a program to read a sequence of positive integer inputs from a user until they enter -1, after which the program should display the smallest and largest inputs, as well as the count of odd and even values among the inputs, excluding the -1 termination value. Here is a step-by-step Python example that accomplishes this task:

  • Initialize variables to hold the smallest and largest values, and counters for even and odd values.
  • Continuously prompt the user to enter a number until they enter -1.
  • Check if each entered number is smaller than the smallest or larger than the largest and update accordingly.
  • Increment the even or odd counter based on the input's divisibility by 2.
  • Once -1 is entered, display the results: smallest, largest, even count, and odd count.

Here's an example code snippet:

smallest = None
largest = None
even_count = 0
odd_count = 0

while True:
num = int(input("Enter a positive integer or -1 to stop: "))
if num == -1:
break
if smallest is None or num < smallest:
smallest = num
if largest is None or num > largest:
largest = num
if num % 2 == 0:
even_count += 1
else:
odd_count += 1

print("Smallest number:", smallest)
print("Largest number:", largest)
print("Number of even values:", even_count)
print("Number of odd values:", odd_count)

This program will correctly fulfill the requirements described in the question.

User Jack Trowbridge
by
8.8k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.