Practice

Sum Until Zero

MEDIUMLoops

Read a series of positive integers from the user. Stop when the user enters 0. Output the total of all numbers entered (not counting the 0). If the first number entered is 0, output 0.

Use a WHILE loop.

Input: A sequence of integers ending with 0. Output: The sum of all non-zero entries.

Example:

Input:  5
        10
        3
        0
Output: 18

Sample Test Cases

Test 1: Basic sum
Inputs: 5, 10, 3, 0
Expected: 18
Test 2: Immediate zero
Inputs: 0
Expected: 0
Test 3: Single number
Inputs: 7, 0
Expected: 7

Premium Question

This question needs a paid plan — upgrade, or join a class from a teacher who has one. You can read the description and sample tests, but grading is locked.

Sign in to get started