0
0
CProgramBeginner · 2 min read

C Program to Find Average of Numbers

To find the average of numbers in C, read the numbers, sum them using a loop, then divide the sum by the count using average = sum / count;.
📋

Examples

Input3 numbers: 5 10 15
OutputAverage = 10.00
Input4 numbers: 2 4 6 8
OutputAverage = 5.00
Input1 number: 7
OutputAverage = 7.00
🧠

How to Think About It

To find the average, first get how many numbers you have. Then add all those numbers together. Finally, divide the total sum by the number of numbers to get the average.
📐

Algorithm

1
Get the count of numbers from the user.
2
Initialize a sum variable to zero.
3
Use a loop to read each number and add it to sum.
4
Calculate average by dividing sum by count.
5
Print the average.
💻

Code

c
#include <stdio.h>

int main() {
    int n, i;
    float num, sum = 0.0, average;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    if (n == 0) {
        printf("Cannot divide by zero. No numbers entered.\n");
        return 1;
    }

    for(i = 0; i < n; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%f", &num);
        sum += num;
    }

    average = sum / n;
    printf("Average = %.2f\n", average);

    return 0;
}
Output
Enter the number of elements: 3 Enter number 1: 5 Enter number 2: 10 Enter number 3: 15 Average = 10.00
🔍

Dry Run

Let's trace the example with 3 numbers: 5, 10, 15 through the code.

1

Input count

User enters n = 3

2

Initialize sum

sum = 0.0

3

First iteration

Read num = 5; sum = 0.0 + 5 = 5.0

4

Second iteration

Read num = 10; sum = 5.0 + 10 = 15.0

5

Third iteration

Read num = 15; sum = 15.0 + 15 = 30.0

6

Calculate average

average = 30.0 / 3 = 10.0

7

Print result

Output: Average = 10.00

IterationInput NumberSum After Addition
155.0
21015.0
31530.0
💡

Why This Works

Step 1: Reading numbers

We read each number one by one using a loop and add it to the total sum stored in sum.

Step 2: Calculating average

After summing all numbers, we divide the sum by the count n to get the average.

Step 3: Displaying result

We print the average with two decimal places using printf formatting.

🔄

Alternative Approaches

Using array to store numbers
c
#include <stdio.h>

int main() {
    int n, i;
    float numbers[100], sum = 0.0, average;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    if (n == 0) {
        printf("Cannot divide by zero. No numbers entered.\n");
        return 1;
    }

    for(i = 0; i < n; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%f", &numbers[i]);
        sum += numbers[i];
    }

    average = sum / n;
    printf("Average = %.2f\n", average);

    return 0;
}
This stores all numbers in an array, useful if you need to use them later, but uses more memory.
Using while loop
c
#include <stdio.h>

int main() {
    int n, i = 0;
    float num, sum = 0.0, average;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    if (n == 0) {
        printf("Cannot divide by zero. No numbers entered.\n");
        return 1;
    }

    while(i < n) {
        printf("Enter number %d: ", i + 1);
        scanf("%f", &num);
        sum += num;
        i++;
    }

    average = sum / n;
    printf("Average = %.2f\n", average);

    return 0;
}
Using a while loop instead of for loop works the same but may be clearer for some beginners.

Complexity: O(n) time, O(1) space

Time Complexity

The program reads each number once in a loop of size n, so time complexity is O(n).

Space Complexity

Only a few variables are used to store sum and counters, so space complexity is O(1).

Which Approach is Fastest?

Both for and while loop approaches have the same time and space complexity; using an array uses more memory but allows reuse of numbers.

ApproachTimeSpaceBest For
Simple loop with sumO(n)O(1)Quick average calculation without storing numbers
Array storageO(n)O(n)When numbers need to be reused later
While loopO(n)O(1)Beginners preferring while loops
💡
Always check that the count of numbers is not zero before dividing to avoid errors.
⚠️
Dividing by zero when no numbers are entered causes a runtime error.