Section 6. Chapter 4
single
Challenge: Finding the Sum Threshold
Swipe to show menu
Task
Swipe to start coding
Find out how many consecutive numbers (starting from 1) must be added together before their running total exceeds a given limit.
For example, if
limit = 15
1 + 2 + 3 + 4 + 5 = 15 → not yet over
1 + 2 + 3 + 4 + 5 + 6 = 21 → now it's over 15
So the answer would be 6 numbers.
-
Use a
whileloop to keep adding numbers until the condition is met. -
Inside the loop, on every iteration:
- Increment
countby1. - Add the current value of
counttosum. - The loop should keep running as long as
sumis less than or equal tolimit.
- Increment
-
Once the loop ends, return
count, the number of terms it took to exceed the limit.
Solution
Everything was clear?
Thanks for your feedback!
Section 6. Chapter 4
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat