65.8k views
3 votes
"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 an example given in this chapter counts the number of positive and negative numbers entered but this problem asks you to fibd the sums of the positive and negative numbers entered Use Raptor in a reverse loop logic with no on left and yes on right"

User NLV
by
6.5k points

1 Answer

5 votes

Answer:

The code solution is written in Python.

  1. positiveSum = 0
  2. negativeSum = 0
  3. num = int(input("Enter a number: "))
  4. while(num != 0):
  5. if(num > 0):
  6. positiveSum += num
  7. else:
  8. negativeSum += num
  9. num = int(input("Enter a number: "))
  10. print("Sum of positive number: " + str(positiveSum))
  11. print("Sum of negative number: " + str(negativeSum))

Step-by-step explanation:

Firstly, we need to create two variables, positiveSum & negativeSum, to hold the running total of the positive and negative input numbers. (Line 1 - 2)

Next, use Python in-built method input() to prompt user for the first input number and assign it to variable num. (Line 4)

Create a while-loop by setting the condition as while input number is not equal to 0, the program shall positive to add the current input number to either variable positiveSum or negativeSum. (Line 6 -10) If the input number is bigger than zero, the current input number should be added to variable positiveSum else it should add to variable negativeSum.

Next, the program shall proceed to prompt user for the next input number. (Line 12)

The entire process is repeated until user enters zero to exit the while loop.

Lastly, display the total of positive and negative numbers. (Line 14 - 15)

User CularBytes
by
5.9k points