Ask Question

Write a program that reads in X whole numbers and outputs (1) the sum of all positive numbers, (2) the sum of all negative numbers, and (3) the sum of all positive and negative numbers. The user can enter the X numbers in any different order every time, and can repeat the program if desired.

+4
Answers (1)
  1. 4 November, 02:24
    0
    run = 'y' sumPos = 0 sumNeg = 0 grandTotal = 0 while run = = 'y': num = input ("Enter a whole number: ") while (num! = 't') : num = int (num) if (num > 0) : sumPos + = num else: sumNeg + = num grandTotal + = num num = input ("Enter a whole number: ") print ("Sum of positive: " + str (sumPos)) print ("Sum of negative: " + str (sumNeg)) print ("Grand total: " + str (grandTotal)) sumPos = 0 sumNeg = 0 grandTotal = 0 run = input ("Do you wish to continue (y/n) : ")

    Explanation:

    Firstly, we get ready all the necessary variables (Line 1 - 4) to track the running status, summation of positive, negative and grand total of all input numbers.

    We create a while loop to keep our program running when run status is still equal to 'y' (Line 9).

    Next we create if else statements to check if the number is positive or negative and add them to either sumPos or sumNeg (Line 11-14). The value of the grandTotal will be accumulated no matter the input number is positive or negative (Line 16). This process will repeat until user input "t".

    Then we can display the sum of positive, negative and all numbers (Line 19-21) and reset the sumPos, sumNeg and grandTotal to 0 (Line 22-24).

    At last we prompt user to input their choice is they wish to continue the program (Line 25).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program that reads in X whole numbers and outputs (1) the sum of all positive numbers, (2) the sum of all negative numbers, and (3) ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers