GuideFoot - Learn Together, Grow Smarter. Logo

In Computers and Technology / High School | 2025-07-03

Write a Python program to input an array of 5 numbers from the keyboard and compute separately the sum of positive numbers and negative numbers.

Asked by slrc3423

Answer (2)

The Python program collects 5 numbers, computes the sums of positive and negative values, and displays the results. It uses loops for input and calculation. This helps in understanding basic programming concepts like lists and conditionals.
;

Answered by Anonymous | 2025-07-04

To solve this problem, we will create a Python program that allows a user to input five numbers. The program will then calculate the sum of positive numbers separately from the sum of negative numbers.
Here's a step-by-step guide to writing the program:

Define the number of elements: We start by defining a variable to store the number of inputs, which is 5 in this case. This can be done using the syntax num_elements = 5.

Initialize variables: We create two variables to store the sums of positive and negative numbers separately. For example:
sum_positive = 0 sum_negative = 0

Input numbers and compute sums: We will loop through the range of 5, asking the user to input numbers at each iteration. Depending on whether the number is positive or negative, we add it to the corresponding sum.
for _ in range(num_elements): number = float(input("Enter a number: ")) if number > 0: sum_positive += number elif number < 0: sum_negative += number

Output the results: Finally, after processing all inputs, we print the sums of positive and negative numbers:
print("Sum of positive numbers:", sum_positive) print("Sum of negative numbers:", sum_negative)


Here's the complete Python program:
num_elements = 5 sum_positive = 0 sum_negative = 0
for _ in range(num_elements): number = float(input("Enter a number: ")) if number > 0: sum_positive += number elif number < 0: sum_negative += number
print("Sum of positive numbers:", sum_positive) print("Sum of negative numbers:", sum_negative)
This program serves as a basic introduction to handling loops, conditional statements, and user input in Python, which are fundamental skills in computer programming.

Answered by OliviaMariThompson | 2025-07-06