Sum of N Numbers
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
C
Hints
Read inside the loop and keep a running total.
Solution explanation
Reading a count first tells you exactly how many numbers follow.
Show solution
#include <stdio.h>
int main() {
int n, x;
int sum = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
sum += x;
}
printf("%d\n", sum);
printf("%.2f\n", (double) sum / n);
return 0;
}