Start learning free

LESSON 218 OF 289 · WHILE LOOPS - CHALLENGES

Sum Until Limit in C#: A while Loop with a Running Total

Practice Read the full explanation

Given a positive integer limit, add up the numbers 1, 2, 3, … in this order, and stop as soon as the total is at least limit. Return how many numbers you added.

The total may go past limit: the number that makes it reach or pass limit is the last one added, and it counts.

 

Example 1:

Input: limit = 10
Output: 4
Explanation: 1 + 2 + 3 + 4 = 10 reaches the limit exactly, so 4 numbers were added.

Example 2:

Input: limit = 11
Output: 5
Explanation: 1 + 2 + 3 + 4 = 10 is still less than 11, so 5 is added too. The total becomes 15, and 5 numbers were added.

Example 3:

Input: limit = 1
Output: 1
Explanation: The first number, 1, already reaches the limit.

 

Constraints:

  • 1 <= limit <= 109
STEP 1 OF 6
    Ctrl + Enter to check

    C# · WHILE LOOPS - CHALLENGES

    Sum Until Limit in C#, explained

    Problem overview

    SumUntilLimit receives a positive int limit. You add the numbers 1, 2, 3, and so on, in that order, and stop as soon as the total is at least limit. The method returns how many numbers you added. The number that makes the total reach or pass the limit counts.

    For limit = 10, 1 + 2 + 3 + 4 = 10 reaches the limit exactly, so the answer is 4. For limit = 11, the total after 4 numbers is 10, still below 11, so 5 is added too. The total becomes 15 and the answer is 5. For limit = 1, the first number is enough: the answer is 1.

    Approach

    Keep two variables: total, the sum so far, and count, how many numbers have been added. Because the numbers go 1, 2, 3 in order, count is also the last number added. So the next number to add is always count + 1.

    The loop runs while total < limit. The moment the total reaches the limit, the condition is false and the adding stops. The condition is checked before each pass, so a pass that pushes the total over the limit still finishes, and its number is counted.

    Step by step

    1. Create total as an int set to 0. Nothing has been added yet.
    2. Create count as an int set to 0.
    3. Write while (total < limit). The loop stops when the total is at least the limit.
    4. Add 1 to count with ++. It is now the next number: 1 on the first pass, 2 on the second.
    5. Add count to total. The order of steps 4 and 5 matters: count goes up first, so the number added is the next one in the sequence.
    6. Return count after the loop.

    For limit = 11:

    Passcounttotal
    111
    223
    336
    4410
    5515

    After pass 5, 15 < 11 is false, so the loop ends and the method returns 5.

    The solution in C#

    class Solution {
        public int SumUntilLimit(int limit) {
            int total = 0;
            int count = 0;
            while (total < limit) {
                count++;
                total += count;
            }
            return count;
        }
    }

    The loop condition uses <, not <=. When total equals limit, the sum has reached the limit, which is the stop condition.

    Why a loop and not a formula

    The sum of the first k numbers has a closed form, k times (k + 1) divided by 2, so you could solve for k with a square root. A loop is simpler and avoids rounding questions. The condition total < limit says exactly what the problem says in words.

    Edge cases and common mistakes

    • Reaching the limit exactly. For 10 the total hits 10 after 4 numbers and the loop stops there.
    • Overshooting. For 11, the total jumps from 10 to 15. The last number counts even though the total passes the limit.
    • Using <=. It would add one extra number whenever the total lands exactly on the limit.
    • Swapped lines. If you add to total before incrementing count, you add the previous number again and the sequence is shifted.
    • Largest input. For 1000000000, the loop runs 44721 times and the total ends a little above the limit, still within the int range.

    Testing

    The solution returns 4 for 10, 5 for 11, 1 for 1 and 44721 for 1000000000.

    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free