In: Computer Science
Write a Python program that:
Hint: For each input number - if the number is < 25 then multiply it times the overall product, else if it is >= 25 then add it to the overall sum. Be sure to initialize the product and sum it to appropriate initial values.
Ok, I will give the answer completely,
python program(I had included comments for better understanding, also please refer the screenshot of the code, attached below , of you are having trouble in understanding the indentation)
# Initialize product and sum initial values
productResult = 1
sumResult = 0
# read the inputs from the user
while True:
n = int(input("Enter a positive integer, 0 to quit : "))
if n<0:
print("Invalid value, enter positive integer")
continue
if n ==0:
break
if n < 25:
productResult = productResult * n
else:
sumResult = sumResult + n
# Now the program is out of the loop, display the results
print(f"The resultant sum : {sumResult}")
print(f"The resultant product : {productResult}")
Screenshot of the code :
Screenshot of the output :
Algorithm :
STEP 1 : INITIALIZE THE VARIABLE productResult AS 1
STEP 2 : INITIALIZE THE VARIABLE sumResult AS 0
STEP 3 : READ N
STEP 4 : REPEAT UNTIL N NOT EQUAL TO 0
STEP 5 : IF N < 0
STEP 6 : THEN, PRINT "Invalid value, enter positive integer" AND GO THE STEP 4
STEP 7 : IF N < 25
STEP 8 : THEN, SET : productResult = productResult * n
STEP 9 : IF N>=25
STEP 10 : THEN, SET : sumResult = sumResult + n
STEP 11 : READ N
STEP 12 : PRINT "The resultant sum :",sumResult
STEP 13 : PRINT "The resultant product :",productResult
FLOWCHART :