{ }codelper
sentinel looprunning totalsP0051

Running total until zero

Integers are given one per line, and a 0 marks the end of the input.
Add the numbers before the 0 one by one, and after each addition print the total so far on its own line.
The 0 itself is not added or printed.
There may be more lines after the 0; ignore them all.
At least one number comes before the 0.
A running total of 0 does not end the input.

Input
1 to 10,000 numbers before the 0, each between -1,000,000,000 and 1,000,000,000 and not 0.
The number of lines after the 0 is unspecified.
Output
one line per number before the 0, each with the total so far
Example 1 · stdin
3
-5
10
0
stdout
3
-2
8
Example 2 · stdin
4
-4
7
0
100
0
stdout
4
0
7
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

A sentinel loop goes 'read → is it 0?
→ process'.
Reading and testing in one line: Python while (x := int(input())) != 0: (the 3.8 walrus operator), C while (scanf("%lld", &x) == 1 && x != 0), C++ while (std::cin >> x && x != 0), Java/C# while ((x = …parse(readLine())) != 0).
JS/TS read all input at once, so break out of the for … of on 0.
Craykon cannot assign inside an expression, so read once before the loop and read the next line again at the end of the loop body (x = Console.ReadLine().ToNumber();).
Three traps: (1) reading to end of input (EOF) also adds the lines after the 0 (second example).
(2) Checking for 0 after processing prints one extra total for the 0.
(3) The total can reach 10^9 × 10^4 = 10^13, so C, C++, Java and C# need long (long long).
Python integers never overflow and JS/TS/Craykon doubles are exact up to 2^53 (about 9×10^15), so they are fine.

Pythonmain.py · 5 lines
total = 0# 0이 끝 표시다. 0 뒤에 줄이 더 있어도 읽지 않는다while (x := int(input())) != 0:    total += x    print(total)
JavaScriptmain.js · 9 lines
const nums = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out = [];let total = 0;for (const x of nums) {  if (x === 0) break; // 끝 표시. 뒤에 남은 수는 무시  total += x;  out.push(total);}console.log(out.join("\n"));
TypeScriptmain.ts · 10 lines
import { readFileSync } from "node:fs";const nums: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out: number[] = [];let total = 0;for (const x of nums) {  if (x === 0) break; // 끝 표시. 뒤에 남은 수는 무시  total += x;  out.push(total);}console.log(out.join("\n"));
JavaMain.java · 17 lines
import java.io.BufferedReader;import java.io.InputStreamReader; public class Main {    public static void main(String[] args) throws Exception {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        StringBuilder out = new StringBuilder();        long total = 0; // 10^9 × 10^4 = 10^13 이라 int로는 넘친다        long x;        // 0이 끝 표시다. 0 뒤의 줄은 읽지 않는다        while ((x = Long.parseLong(br.readLine().trim())) != 0) {            total += x;            out.append(total).append('\n');        }        System.out.print(out);    }}
C#Main.cs · 19 lines
using System;using System.Text; class Program{    static void Main()    {        var out_ = new StringBuilder();        long total = 0; // 10^9 × 10^4 = 10^13 이라 int로는 넘친다        long x;        // 0이 끝 표시다. 0 뒤의 줄은 읽지 않는다        while ((x = long.Parse(Console.ReadLine())) != 0)        {            total += x;            out_.Append(total).Append('\n');        }        Console.Write(out_);    }}
C++main.cpp · 12 lines
#include <iostream> int main() {    std::ios::sync_with_stdio(false);    std::cin.tie(nullptr);    long long x, total = 0; // 10^9 × 10^4 = 10^13 이라 int로는 넘친다    // 0이 끝 표시다. 0 뒤의 줄은 읽지 않는다    while (std::cin >> x && x != 0) {        total += x;        std::cout << total << '\n';    }}
Cmain.c · 11 lines
#include <stdio.h> int main(void) {    long long x, total = 0; /* 10^9 × 10^4 = 10^13 이라 int로는 넘친다 */    /* 0이 끝 표시다. 0 뒤의 줄은 읽지 않는다 */    while (scanf("%lld", &x) == 1 && x != 0) {        total += x;        printf("%lld\n", total);    }    return 0;}
Craykonmain.cray · 9 lines
합 = 0;x = Console.ReadLine().ToNumber();// 0이 끝 표시다. 0 뒤의 줄은 읽지 않는다while (x != 0){    합 += x;    Console.WriteLine(합);    x = Console.ReadLine().ToNumber();}