Sum Until Zero

Sum Until Zero

Easy Python Control Flow 11 views
Explanation Complexity

Problem Statement

A list of integers is provided. Add numbers one by one until you see 0. Do not include 0 in sum. Output the sum.

Input Format

• First line: Space-separated integers

Output Format

• Print one integer: sum of numbers until 0 appears

• Do not include 0 in the sum

Example

5 8 3 0 7 9
16

Constraints

• List size ≤ 10^5

• -10^9 ≤ numbers ≤ 10^9

• It is guaranteed that 0 will appear at least once

Concept Explanation

We add numbers until we see 0.

5 + 8 + 3 = 16

Stop when 0 appears.
Do not include 0 in sum.

Step-by-Step Explanation

1.Read the list of integers.

2.Initialize total = 0.

3.Loop through each number in the list:

• If number is 0, break the loop.

• Otherwise, add it to total.

4.Print total.

Concept Explanation

We add numbers until we see 0.

5 + 8 + 3 = 16

Stop when 0 appears.
Do not include 0 in sum.

Step-by-Step Explanation

1.Read the list of integers.

2.Initialize total = 0.

3.Loop through each number in the list:

• If number is 0, break the loop.

• Otherwise, add it to total.

4.Print total.

Input / Output Format

Input Format
• First line: Space-separated integers
Output Format
• Print one integer: sum of numbers until 0 appears

• Do not include 0 in the sum
Constraints
• List size ≤ 10^5

• -10^9 ≤ numbers ≤ 10^9

• It is guaranteed that 0 will appear at least once

Examples

Input:
5 8 3 0 7 9
Output:
16

Example Solution (Public)

Python
import sysnarr=sys.stdin.read().strip().split()nif not arr: sys.exit(0)ntotal=0nfor tok in arr:n  v=int(tok)n  if v==0:n    breakn  total+=vnsys.stdout.write(str(total))

Official Solution Code

import sysnarr=sys.stdin.read().strip().split()nif not arr: sys.exit(0)ntotal=0nfor tok in arr:n  v=int(tok)n  if v==0:n    breakn  total+=vnsys.stdout.write(str(total))
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.