{ }codelper
reading until EOFsumP0018

Sum to the end

Integers are given one per line, but you are not told how many lines there are.
Read every number until the end of input and print the sum.
The input has at least one line and no blank lines.
The last line may or may not end with a newline.

Input
1 to 100,000 lines, -10,000 ≤ each number ≤ 10,000
Output
the sum on one line
Example 1 · stdin
3
-1
10
stdout
12
Example 2 · stdin
5
stdout
5
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

How you notice "input has ended" differs by language.
Python for line in sys.stdin: (the loop just stops — calling input() again raises EOFError), Java BufferedReader.readLine() and C# Console.ReadLine() return null at the end, C while (scanf("%d", &x) == 1) (scanf returns EOF at the end), C++ while (std::cin >> x), and in JS/TS you read the whole input at once and split it.
In Craykon Console.ReadLine() returns null at the end, so loop while (line != null).
The classic C trap is while (!feof(stdin)) — feof becomes true only *after* a read has failed, so after the failed scanf past the final newline the leftover x is added once more (the first example gives 22 instead of 12).
Do not read the first line as a count as in earlier problems.

Pythonmain.py · 6 lines
import sys total = 0for line in sys.stdin:  # 입력이 끝나면 반복도 끝난다    total += int(line)print(total)
JavaScriptmain.js · 2 lines
const nums = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log(nums.reduce((s, x) => s + x, 0));
TypeScriptmain.ts · 3 lines
import { readFileSync } from "node:fs";const nums: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log(nums.reduce((s, x) => s + x, 0));
JavaMain.java · 13 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));        int total = 0;        String line;        // readLine()은 입력이 끝나면 null        while ((line = br.readLine()) != null) total += Integer.parseInt(line.trim());        System.out.println(total);    }}
C#Main.cs · 13 lines
using System; class Program{    static void Main()    {        int total = 0;        string line;        // ReadLine()은 입력이 끝나면 null        while ((line = Console.ReadLine()) != null) total += int.Parse(line);        Console.WriteLine(total);    }}
C++main.cpp · 9 lines
#include <iostream> int main() {    std::ios::sync_with_stdio(false);    std::cin.tie(nullptr);    int x, total = 0;    while (std::cin >> x) total += x; // 읽기에 실패하면(입력 끝) 거짓    std::cout << total << '\n';}
Cmain.c · 9 lines
#include <stdio.h> int main(void) {    int x, total = 0;    /* scanf는 수를 읽은 개수를 돌려준다 — 입력이 끝나면 EOF(-1)라 1이 아니다 */    while (scanf("%d", &x) == 1) total += x;    printf("%d\n", total);    return 0;}
Craykonmain.cray · 8 lines
합 = 0;줄 = Console.ReadLine();while (줄 != null){    합 += 줄.ToNumber();    줄 = Console.ReadLine();}Console.WriteLine(합);