In this exercise you will create a program that computes the average of a collection of values entered by the user. The user will enter 0 as a sentinel value to indicate that no further values will be provided. Your program should display an appropriate error message if the first value entered by the user is 0.Hint: Because the 0 marks the end of the input it should not be included in the average.
Question
In this exercise you will create a program that computes the average of a collection of values entered by the user. The user will enter 0 as a sentinel value to indicate that no further values will be provided. Your program should display an appropriate error message if the first value entered by the user is 0.Hint: Because the 0 marks the end of the input it should not be included in the average.
Solution
Sure, here is a simple Python program that accomplishes this task:
def compute_average():
total = 0
count = 0
while True:
num = float(input("Enter a number (0 to end): "))
if num == 0:
if count == 0:
print("Error: The first number cannot be 0.")
continue
else:
break
total += num
count += 1
average = total / count
print("The average is:", average)
compute_average()
This program works by continuously asking the user for a number until they enter 0. If the first number entered is 0, it prints an error message and asks for a number again. Otherwise, it adds the number to a running total and increments a count of the numbers entered. When the user enters 0 (and it's not the first number), it breaks out of the loop, computes the average by dividing the total by the count, and prints the average.
Similar Questions
Write a program that repeatedly asks for numbers. When the user enters 0, the program prints the average of all numbers entered (excluding the 0) accurate to two decimal places and exits.
code this in java using patterns: Specification: Read in integers until the user enters -1. If there was at least one integer, show the average value.
Write a C program to compute the maximum marks and average marks of a class using sentinel controlled repetition (-1 as the sentinel value)?- If user enters negative marks then display INVALID INPUT and and read the marks again.- If number of students is 0, then display "NO DATA"- If number of students > 0, then display maximum marks and the average marks separated by space.-You can assume that marks are integers and average also you discard decimal value.
import java.util.Scanner;public class Test1 {public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three numbers: "); float number1 = input.nextInt(); float number2 = input.nextInt(); float number3 = input.nextInt(); // Compute average double average = (number1 + number2 + number3) / 3; // Display result System.out.println(average); }}When a user enters 1 2 3, during the program execution what will be the exact output? Group of answer choices2.03.0The program gives an error1.0
Initialize an array/list with five integers. Write a program to find the sumand average of these numbers
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.