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("Enter the value of n:");
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 Using while loop
#include<stdio.h>
#include<conio.h>
void main() {
int n, i = 1;
float num, average, sum = 0;
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 1:
[wpusb]