Write a program that computes the total, i.e. sum, of a given list of numbers.
Input begins with a line containing an integer n indicating the number of lines that follow. The number n should not be included in the sum. Each of the n following lines contain an integer xₘ to be accounted for the sum.
Your program should work for n and xₘ in the following ranges:
1 ≤ n ≤ 1000
0 ≤ xₘ ≤ 100 000
Output should contain a single line with an integer t = x₁ + x₂ + x₃ + … + xₙ.
3
1
2
3
6
5
10
20
30
40
50
150
This exercise can be solved without storing all values in memory at the same time, i.e.: without resorting to arrays or lists. – O(1) memory usage.
In this exercise the input numbers are given across multiple lines of input.
try first: hello2
try next: countdown1
Copyright © 2020-2022 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/total