C Program to find the Average of numbers with explanations

In this tutorial, we are going to learn a writing program in the C Programming language to find the Average of numbers.

This is How our program will behave

1). At first Our C Program will take the total count for the numbers for which we want to calculate the average.

2). After taking the count, We have to give the number to calculate the average.

3). Here we are considering input value as Integer and output may be an integer or float value.

What is the average number?

Average is a mathematical calculation that can be obtained by the sum of numbers divided by the total count of the numbers.

For example

Suppose we want to calculate the Average of 1,2,3,4,5

So, first, find the sum of all numbers

1+2+3+4+5 = 15

And here total count for the numbers is 5.

So Average will be 15/5 = 3

C Program to Calculate the average of numbers using for loop

#include<stdio.h>
void main() {
  int n, i;
  float num, average, sum = 0;
  printf("=== C Program to Calculate the average of numbers === \n");
  printf("How many numbers you have to Calculate average?:");
  scanf("%d", & n);
  for (i = 1; i <= n; i++) {
    printf("Enter the %d number: ", i);
    scanf("%f", & num);
    sum += num;
  }
  average = sum / n;
  printf("\nTotal Average is = %1.2f\n ", average);
}

Output

=== C Program to Calculate the average of numbers === 
How many numbers you have to Calculate average?:5
Enter the 1 number: 3
Enter the 2 number: 4
Enter the 3 number: 2
Enter the 4 number: 1
Enter the 5 number: 7

Total Average is = 3.40

C Program to calculate the average of numbers Using while loop

#include<stdio.h>
#include<conio.h>
void main() {
  int n, i = 1;
  float num, average, sum = 0;
  printf("=== C Program to Calculate the average of numbers === \n");
  printf("Enter the value of n:");
  scanf("%d", &n);
  while (i <= n) {
    printf("Enter the %d number: ", i);
    scanf("%f", &num);
    sum += num;
    i++;
  }
  average = sum / n;
  printf("\nTotal Average is = %1.2f\n ", average);
}

Output:

=== C Program to Calculate the average of numbers === 
Enter the value of n:6
Enter the 1 number: 43
Enter the 2 number: 44
Enter the 3 number: 21
Enter the 4 number: 22
Enter the 5 number: 78
Enter the 6 number: 2

Total Average is = 35.00
What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now