C Average of N Numbers Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is the Average of N Numbers?

The average (arithmetic mean) of N numbers is their sum divided by the count.

Remember
average = (a₁ + a₂ + … + aₙ) / N

Example: 1, 2, 3, 4, 5
  sum = 15, N = 5
  average = 15 / 5 = 3
ValuesSumNAverage
1, 2, 3, 4, 51553
10, 15, 20, 25, 30100520
2, 4623
7717

How to Find the Average of N Numbers in C

Sum every value once, then divide by the count — guard empty input first.

  1. Reject N <= 0 (no division by zero).
  2. Read or iterate the N values.
  3. Accumulate into a double sum.
  4. Return sum / (double) N.
MethodIdeaTimeBest for
User input loopscanf N times, running sumO(N)Console demos
Array helperSum array, then divideO(N)Interviews, default
Compact mainInline sum in mainO(N)Tiny samples

Pseudocode

Pseudocode
function Average(values, N):
    if N <= 0:
        return error   // or a documented sentinel

    sum = 0
    for each value in values:
        sum = sum + value

    return sum / N

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Formulaaverage = sum / N
TypeUse double for fractional means
GuardNever divide when N <= 0
Not medianMedian needs sorting; average does not
1..5sum 15 → average 3

Live Preview

Enter comma-separated numbers to see the sum, count, and average.

Comma-separated numbers. Tap a chip or type your own list.

Result
values = [1, 2, 3, 4, 5]
sum = 15, N = 5
average = 3

Worked Walkthrough — 1, 2, 3, 4, 5

Add every value, then divide by the count.

Calculation
values: 1, 2, 3, 4, 5
N = 5

sum = 0
  + 1 → 1
  + 2 → 3
  + 3 → 6
  + 4 → 10
  + 5 → 15

average = 15 / 5 = 3

C Programs

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;
}

How It Works

  1. Return 0.0 if n <= 0 (documented sentinel).
  2. Prompt and read each value with scanf("%lf", ...).
  3. Add every value into a double running total.
  4. Divide by n cast to double so the mean keeps decimals.
  5. 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;
}

How It Works

  1. Guard empty or invalid length with a sentinel return.
  2. Sum every element in one forward pass.
  3. Divide by n for the arithmetic mean.
  4. sizeof(data) / sizeof(data[0]) gives the count safely.
  5. 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;
}

How It Works

  1. Detect an empty array before any division.
  2. Sum with a plain for loop.
  3. Print sum / n with %g (shows 3 for this sample).
  4. Same math as the helper — just all in main.
  5. 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.

Time and Space Complexity

ProgramTimeExtra space
Input / array averageO(N)O(1) beyond the data
Storing all inputsO(N)O(N)
Running sum onlyO(N)O(1)

One pass is enough. You do not need to sort for an arithmetic mean.

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.

Next: Biggest of Three Numbers

Find the largest among three values with simple comparisons.

Biggest of three tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

18 people found this page helpful