For a list of numbers entered by the user and terminated by 0. Write a program to find the sum of the positive numbers and the sum of the negative numbers. (Hint: This is similar to an example in this chapter. Be sure to notice that the example given in this chapter counts the number of positive and negative numbers entered, but this problem asks you to find the sums of the positive and negative numbers entered Use Raptor in a reverse loop logic with no on left and yes on right.)

Respuesta :

Limosa

Answer:

Following are the program in the Python Programming Language

#set variables to 0

positive_sum=0

negative_sum=0

#print message

print("Enter 0 to terminate")

#set the while loop

while(True):

 #get input from the user

 num=float(input("Enter positive or negative numbers: "))

 #set if statement to check condition

 if(num==0):

   break

 elif(num>0):

   positive_sum+=num

 else:

   negative_sum+=num

#print output with message

print()

print("sum of positive numbers: ", positive_sum)

print("sum of negative numbers: ", negative_sum)

Output:

Enter 0 to terminate

Enter positive or negative numbers: 1

Enter positive or negative numbers: 3

Enter positive or negative numbers: 5

Enter positive or negative numbers: -7

Enter positive or negative numbers: -2Enter positive or negative numbers: 0

sum of positive numbers:  9.0

sum of negative numbers:  -9.0

Explanation:

Here, we set two integer data type variables "positive_sum", "negative_sum" and initialize to 0.

Then, we set the while infinite loop inside the loop.

  • Set a variable "num" and get input from the user in it.
  • Set the if conditional statement and check condition the variable "num" is equal to 0 then, loop will terminate.
  • Set the elif statement and check condition the variable "num" is greater than 0 then, add that input and store in the variable "positive_sum"
  • Otherwise, add that input and store in the variable "negative_sum".

Finally, we print the output with the message.