4.2 code practice: question 2 in python
Answers
Write a PYTHON program to solve the following problem.
Question:
Write a program that inputs numbers and keeps a running sum. When the sum is greater than 100, output the sum as well as the count of how many numbers were entered.
Sample run:
Enter a number: 1
Enter a number: 41
Enter a number: 36
Enter a number: 25
Sample output:
Total sum: 103
Total number of numbers entered: 4
Program (Dynamic):
totalsum=0
count=0
while(totalsum<=100):
num=int(input("Enter a number: "))
totalsum=totalsum+num
count=count+1
print("Total sum: {0}".format(totalsum))
print("Total number of numbers entered: {0}".format(count))
Input:
Enter a number: 1
Enter a number: 41
Enter a number: 36
Enter a number: 25
Output:
Total sum: 103
Total number of numbers entered: 4
Explanation:
Here, totalsum gives the sum value when the sum of entered numbers becomes greater than 100, where the variable count gives the number of numbers that have been entered which together made the sum greater than 100. Initialize both the values with 0.
Here, we run a while loop which checks the condition totalsum<=100 on every iteration. On every iteration, it takes a number, adds it to the totalsum, and increments the count by 1. Once, the total becomes >100, the loop gets terminated and the corresponding values of totalsum and numbers count are printed.
Note:
As this program is written dynamically that supports the user to give the values at run time, you can input any values (integers) as per your wish.
Learn more:
1. Raju has a square-shaped puzzle made up of small square pieces containing numbers on them. He wants to rearrange the puzzle by changing the elements of a row into a column element and column element into a row element. Help Raju to solve this puzzle.
https://brainly.in/question/16956126
2. Printing all the palindromes formed by a palindrome word.
brainly.in/question/19151384