index | submit | rank | book

total – Total

Write a program that computes the total, i.e. sum, of a given list of numbers.

If I have one banana, two oranges and three tomatoes, how many items to I have?

Input and Output

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ₙ.

Example input 1

3
1
2
3

Example output 1

6

Example input 2

5
10
20
30
40
50

Example output 2

150

Scoring

Hints

  1. 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.

  2. In this exercise the input numbers are given across multiple lines of input.

try first: hello2

try next: countdown1

index | submit | rank | book

Copyright © 2020-2022 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/total