173k views
1 vote
Write a program that read a sequence of positive integer inputs and print the sum of all the input numbers greater than 50. Then your program will also compute the average. Assume that the user always enters a positive number (integer) as an input value or a negative integer as a sentinel value.

1 Answer

2 votes

Final answer:

The program reads positive integers, sums those greater than 50, and when a negative integer is entered, it computes and prints the average of those numbers.

Step-by-step explanation:

Program to Calculate Sum and Average of Numbers Greater Than 50

The task is to write a program that reads a sequence of positive integers, calculates the sum of all the numbers greater than 50, and then computes the average of these numbers. The program will continue to read numbers until a negative integer, which is used as a sentinel value, is entered. Here's a sample program written in Python:

total_sum = 0
count = 0
while True:
number = int(input("Enter a positive number or a negative number to stop: "))
if number < 0:
break
if number > 50:
total_sum += number
count += 1
if count > 0:
average = total_sum / count
print("Sum of numbers greater than 50:", total_sum)
print("Average of numbers greater than 50:", average)
else:
print("No numbers greater than 50 were entered.")

This program uses a while loop to continuously prompt the user for input until a negative integer is entered. It keeps a running total and count of the numbers greater than 50. After the loop terminates, if the count is greater than zero, the program calculates the average and prints out the sum and average.

User Ali Hadjihoseini
by
7.5k points