8684. Sum and amount

 

A sequence of integers is given. Find the number of integers in the sequence and their sum.

 

Input. One single line contains a sequence of integers, each does not exceed 100 in absolute value.

 

Output. Print two numbers in one line: the number of elements in the sequence and their sum.

 

Sample input

Sample output

1 2 3 4 5

5 15

 

 

SOLUTION

array

 

Algorithm analysis

Read the input sequence of numbers until the end of the file. Then, count the number of integers and compute their sum.

 

Algorithm implementation

Read the input sequence of numbers until the end of the file. The variable c stores the count of numbers, and the variable s stores their sum.

 

c = s = 0;

while (scanf("%d", &x) == 1)

{

  c++;

  s = s + x;

}

 

Print the count of numbers and their sum.

 

printf("%d %d\n", c, s);

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int c = 0, s = 0;

    while(con.hasNextInt())

    {

      int x = con.nextInt();

      c++;

      s = s + x;

    }

    System.out.println(c + " " + s);

    con.close();

  }

}

 

Python implementation

Read the input sequence of integers into the list lst.

 

lst = list(map(int, input().split()))

 

Print the count of integers in the list lst and their sum.

 

print(len(lst), sum(lst))