C Program to Calculate Arithmetic Mean

In this article, you will learn and get code about how to find arithmetic mean of given list of number by user (at run-time).

How Arithmetic Mean gets Calculated ?

To calculate arithmetic mean, use the formula mean = (n1+n2+n2+...+nn)/n. Here n1 represents first number, n2 represents second number and so on. Whereas, n (in denominator part) is a value that represents the quantity of number. For example, if there are three numbers say 3, 5, 8. So to find its arithmetic mean, use the formula as given earlier. Therefore, arithmetic mean of 3, 5, 8 will be (3+5+8)/3 or 16/3 or 5.33.

Arithmetic Mean Program in C

To calculate arithmetic mean in C programming, you have to ask from user to enter, how many numbers he/she want to provide (quantity) say 3. Then ask to enter all the numbers (say 3, 5, 8) of that size (3). Now apply the simple formula of finding arithmetic mean as shown here in the following program:

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, size;
    float num, sum, am;
    printf("How many number to enter ? ");
    scanf("%d", &size);
    sum = 0;
    printf("Enter %d Numbers: ", size);
    for(i=0; i<size; i++)
    {
        scanf("%f", &num);
        sum = sum+num;
    }
    am = sum/size;
    printf("\nArithmetic Mean = %0.2f", am);
    getch();
    return 0;
}

This program is compiled and executed using Code::Blocks IDE. Here is the sample run of above program:

c program arithmetic mean

Now supply the size (how many number user want to enter to find arithmetic mean of these entered numbers) say 3. Then enter all the 3 numbers say 3, 5, 8 and finally press ENTER key to see the output as shown in the snapshot given below:

arithmetic mean program c

Steps used in previous Program

Here are the list of some main steps used in above program:

  • Declare any two variable of int (integer type) say size and i
  • Declare another three variables of float (floating-point type) say num, sum, and am
  • Here num is used to store value of number one by one, sum is used to store summation of numbers, and am is used to store arithmetic mean value of all the numbers given by user
  • Scan size from user, that is how many number, he/she want to supply
  • Now create a for loop, to scan all the numbers of that size. For example, if user supply 10 as the size, then create a loop, to run 10 times to scan all the 10 numbers one by one
  • After each scan, apply the summation operation, so that after exiting from the loop, you will have a variable named sum that holds the summation of all the numbers given by user
  • Now just apply the arithmetic mean formula and print the value of am as output. That it
  • The %0.2f format specifier is used to print value upto 2 decimal places only

Same Program in Other Languages

C Online Test


« Previous Program Next Program »