Design a program that ask the user to enter a series of positive numbers. The user should enter a negative number to the series. After all the positive numbers have been entered the program should display their sum

Answer :

frknkrtrn

Answer:

total = 0

while True:

   number = float(input("Enter a number: "))

   

   if number < 0:

       break

   

   total += number

   

print("The total of the positive numbers entered is", total)

Explanation:

*The code is in Python.

Initialize the total as 0

Create a while loop. Inside the loop:

Ask the user to enter a number. Check the number. If it is smaller than 0 (This means it is a negative number), stop the loop using break. Add the number to the total (cumulative sum). (This way your program keeps summing numbers until you enter a negative number)

When the loop is done, print the total

Other Questions