Skip to content
Home » Programming » C Examples » C Program to find the average of N Numbers

C Program to find the average of N Numbers

C program to find the average/mean of N numbers

In this example, we will learn a program to find the average/mean of N numbers in C.

How will we find the average?

To find the average/mean of a number we have to divide the total sum of numbers by the total N (where N is count of how many numbers there are).

Average = Total Sum / N

So, at first we will have to find the total sum of all the numbers. Then finally we will divide the sum by N.

Example:

Let's say we have 5 numbers; 20, 2, 8, 7, 3
sum = 20 + 2 + 8 + 7 + 3 = 40
average = 40 / 5 = 8

Algorithm to find the average of N numbers

Let’s see the algorithm now, which will help us to find the average.

Step 1: Start.
Step 2: Take the number of elements as input in n.
Step 3: Initialize i = 0.
Step 4: Repeat step 4 to step 7 until i < n.
Step 5: Take elements as input in num
Step 6: Calculate,
            sum = sum + num
Step 7: Increment i by 1,
            i = i + 1
Step 8: Finally divide sum by n,
            avg = sum / n
Step 9: End.

In short,

Start,
    n = number of elements
    i = 0
    Repeat until i < n
        Take number as input in num
        sum = sum + num
        i = i + 1
     avg = sum / n
End.

Also Read: C Program to find the Factorial of a number

Program to find the average of N numbers using for loop

#include<stdio.h>

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

    printf("Enter number of elements:  ");
    scanf("%d", &n);
    printf("\nEnter %d elements\n", n);
    for(i = 0; i < n; i++)
    {
        scanf("%f", &num);
        sum = sum + num;
    }
    avg = sum / n;
    printf("\nAverage of numbers is %f", avg);

    return 0;
}

Output

Enter number of elements:  5

Enter 5 elements
10
12
15
5
8
                                                                         
Average of numbers is 10.000000

Explanation

  • Starting, we took the number of the elements (or numbers) of which we will find the average.
printf("Enter number of elements: ");
scanf("%d", &n);

The number of elements is stored in variable n.

  • Then, we took the n numbers as input. To repeat the steps, we are using for loop here. 
for(i = 0; i < n; i++)
{
    . . .
}

We will repeat until i<n which means that we will repeat the loop for n inputs.

  • Inside for loop we are taking the input of all numbers in variable num and adding it with the sum.
sum = sum + num

Which can also be written as,

sum += num

Those two are the same.

  • After the for loop, we have the total sum of numbers, so we divided it with the n to find the average.
avg = sum / n
  • Finally, we printed the average value of n numbers.

Program Using while loop

#include<stdio.h>

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

    printf("Enter number of elements:  ");
    scanf("%d", &n);
    printf("\nEnter %d elements\n", n);
    i = 0;
    while(i < n)
    {
        scanf("%f", &num);
        sum = sum + num;
        i++;
    }
    avg = sum / n;
    printf("\nAverage of numbers is %f", avg);

    return 0;
}

Output

Enter number of elements: 3

Enter 3 elements
5
24
7

Average of numbers is 12.000000

Explanation

This implementation is almost same as previous one (i.e. using for loop) only minor syntax difference can be noticed. Let’s go through those differences only for now.

In for loop, initialization, test condition and update is done in same line whereas in while loop they are in different places.

i = 0;
while(i < n)
{
    . . .
    i++;
}

As you can see here, at first we have initialized the variable i.e i=0. After that in while statement we are checking the condition i<n which checks and repeats the loop for n times to take input and add that number to sum.

scanf("%f", &num);
sum = sum + num;

In every loop, we are taking a variable as input in num and adding it to the variable sum. Also, we are updating the variable i i.e. i++.

Finally, we calculated average value,

avg = sum / n;

Program using do…while loop

#include<stdio.h>

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

    printf("Enter number of elements:  ");
    scanf("%d", &n);
    printf("\nEnter %d elements\n", n);
    i = 0;
    do
    {
        scanf("%f", &num);
        sum = sum + num;
        i++;
    } while(i < n);
    avg = sum / n;
    printf("\nAverage of numbers is %f", avg);

    return 0;
}

Output

Enter number of elements: 4

Enter 4 elements
32
54
23
45

Average of numbers is 38.500000

Explanation

do…while loop and while loops are almost same in terms of syntax but they differ in the working. while loops are entry controlled loops which means that condition is check before entering the loop whereas do…while loops are exit controlled loops. Exit controlled loops are those in which the condition is check at last of the loop.

i = 0;
do
{
    . . .
    i++;
} while(i < n);

As you can see, we have initialization before loop then the update statement and at last we have the test condition. Here test condition i<n ensures that we input n numbers for calculation. We calculate sum inside the loop.

sum = sum + num

Input is taken in variable num and added in sum to find the total sum. Finally, we find the average value as,

avg = sum / n

More Examples

to swap two numbers without using a temporary variable
C Program to find the size of data types using sizeof operator

Leave a Reply

Your email address will not be published. Required fields are marked *