Read the question, write C on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
The first number is n, then n integers. Print their average, rounded to exactly two decimals.
Watch the integer-division trap: divide by n as a double, or the fraction is lost.
Input. n, then n integers (whitespace-separated).
Output. The average with two decimals, e.g. 5.00.
Constraints
- 1 ≤ n ≤ 1000
Examples
Input
4 2 4 6 8
Output
5.00
Input
3 1 2 2
Output
1.67
Hint
- Accumulate the total in a long long as you read.
- Cast before dividing: (double)sum / n, then print with %.2f.
Show correct code
Peek only after you have tried. You can still Check your own version.
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
long long sum = 0;
int x;
for (int i = 0; i < n; i++) { scanf("%d", &x); sum += x; }
printf("%.2f\n", (double)sum / n);
return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.