Sum and Average
Completed
Read n, then n integers. Print their sum and their average with 2 decimals.
Input
n, then n integers.
Output
Two lines.
Example
Input
4 10 20 30 41
Output
101 25.25
Editor
Java
Hints
Keep a running total.
Cast to double before dividing.
Solution explanation
Using long for the total avoids overflow with many large numbers.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long sum = 0;
for (int i = 0; i < n; i++) {
sum += sc.nextInt();
}
System.out.println(sum);
System.out.printf("%.2f%n", (double) sum / n);
}
}