125k views
5 votes
[Edhesive] 4. 2 Code Practice: Question 1

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


Sum: 103

Numbers Entered: 4


Hint: If you get an EOF error while running the code you've written, this error likely means you're asking for too many inputs from the user

User Mtrovo
by
8.8k points

1 Answer

3 votes

Python:

#Sum and count variable.

sum, count = 0, 0

#While loop

while(sum<100):

sum += int(input("Enter a number: "))

count += 1

#Print stats.

print(f"Sum: {sum}\\Numbers Entered: {count}")

C++:

#include <iostream>

int main(int argc, char* argv[]) {

// Sum and count variable.

int sum, count, temp;

// While loop

while (sum < 100) {

std::cout << "Enter a number: ";

std::cin >> temp;

sum += temp;

count++;

}

// Print stats.

std::cout << "Sum: " << sum

<< "\\Numbers Entered: " << count

<< std::endl;

return 0;

}

User Martin Cron
by
8.5k points