Start with console input, then an array helper, and finish with a compact sum loop in main.
Example 1 — Average from User Input
Validate N, loop N times with scanf, keep a running total, then divide.
C
#include <stdio.h>
double averageOfNNumbers(int n) {
int i;
double total = 0.0;
double value;
if (n <= 0) {
return 0.0;
}
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++) {
if (scanf("%lf", &value) != 1) {
return 0.0;
}
total += value;
}
return total / (double) n;
}
int main(void) {
int n = 5;
double result = averageOfNNumbers(n);
printf("The average of the entered numbers is: %.6f\n", result);
return 0;
}
Output
Enter 5 numbers:
1
2
3
4
5
The average of the entered numbers is: 3.000000
How It Works
Return 0.0 if n <= 0 (documented sentinel).
Prompt and read each value with scanf("%lf", ...).
Add every value into a double running total.
Divide by n cast to double so the mean keeps decimals.
Sample input 1…5 prints 3.000000.
Example 2 — Average from an Array
Loop the array once — short and interview-friendly.
C
#include <stdio.h>
double averageFromArray(const double values[], int n) {
int i;
double total = 0.0;
if (n <= 0) {
return 0.0;
}
for (i = 0; i < n; i++) {
total += values[i];
}
return total / (double) n;
}
int main(void) {
double data[] = { 10, 15, 20, 25, 30 };
int n = (int)(sizeof(data) / sizeof(data[0]));
double avg = averageFromArray(data, n);
printf("Average = %.6f\n", avg);
return 0;
}
Output
Average = 20.000000
How It Works
Guard empty or invalid length with a sentinel return.
Sum every element in one forward pass.
Divide by n for the arithmetic mean.
sizeof(data) / sizeof(data[0]) gives the count safely.
10+15+20+25+30 = 100 → average 20.
Example 3 — Compact Sum Loop
A tiny main that sums a fixed array — useful when values are already known.
C
#include <stdio.h>
int main(void) {
double data[] = { 1, 2, 3, 4, 5 };
int n = (int)(sizeof(data) / sizeof(data[0]));
int i;
double sum = 0.0;
if (n == 0) {
printf("No average for an empty array.\n");
} else {
for (i = 0; i < n; i++) {
sum += data[i];
}
printf("%g\n", sum / (double) n);
}
return 0;
}
Output
3
How It Works
Detect an empty array before any division.
Sum with a plain for loop.
Print sum / n with %g (shows 3 for this sample).
Same math as the helper — just all in main.
Good for quick demos; prefer a helper in interviews.
Edge Cases & Pitfalls
Check these before calling the solution done.
Zero
Never divide by N = 0
Empty input is undefined. Return an error or a documented sentinel.
int / int
Avoid integer division
10 / 4 is 2 in integer math. Cast to double first.
Overflow
Sum can grow large
Prefer double (or a wider type) when values or N are large.
Median
Different from median
Average is sum/count. Median needs a sorted middle value.
Analysis
Time and Space Complexity
Program
Time
Extra space
Input / array average
O(N)
O(1) beyond the data
Storing all inputs
O(N)
O(N)
Running sum only
O(N)
O(1)
One pass is enough. You do not need to sort for an arithmetic mean.
Remember
Key Takeaways
Formula: average = sum / N.
Guard: never divide when N is zero.
Type: use double for fractional results.
Test: 1…5 → 3; 10…30 step 5 → 20.
One line: add every value, divide by the count — that is the average.
Frequently Asked Questions
Add all N numbers, then divide by N. Formula: average = sum / N.
Division by zero is invalid. If N is 0, the average is undefined.
Use double for the sum and result when you want decimals. Integer division truncates (10 / 4 becomes 2).
O(N), because each number is visited once to build the sum.
No. Average is sum/count. Median is the middle value after sorting.