write a program to read the following set of data and print sum of positive numbers only.
Data- -4,5,-6,2,4,-10,-8,9,-1
pls answer asap, I will be reporting the false answers.
Answers
Answered by
96
The following code has been written using Python.
data = [-4, 5 ,-6, 2, 4, -10, -8, 9, -1]
s = 0
for i in data:
if i > 0:
s = s + i
print(s, "is the sum of the positive numbers in the given data.")
What we did is, we stored the data in the form of a list and then start a for loop to traverse through the list. A for loop is an iteration statement that does repeated-checking till it reaches the end of a specified range/until a specific condition is met.
In this case, the loop does repeated-checking till the last element in the list. As the loop is continuing, it tests the if condition, which checks if the number is positive/negative. It then gets added to 's' which represents the sum of the positive numbers.
Similar questions