C Program to Find Average of N Numbers using For loop | Array | Functions | While loop
C program to find average of N numbers using for loop, array, functions, and while loop.
In this article, you will learn how to find average of N numbers in the C language using for loop, array, functions, and while loop.
Example
Enter the number of elements to calculate the average::
4
Enter 4 elements one by one
646
642
656
435
The average of the entered input numbers is = 594.750000
You should have knowledge of the following topics in c programming to understand these programs:
- C
main()
function - C
printf()
function - C loops
- C Array
- C Functions
In this article, we solve this problem in six methods:
- Using the for loop
- Using the while loop
- Using the do-while loop
- Using the Array
- Using the functions
- Using the Array and function
Source Code
// C Program to Find Average of N Numbers using For loop
#include <stdio.h>
int main() {
int x, i;
float avg = 0, y;
// x - To store number of elements
// avg - To store total average value
// y - To store total input numbers
printf("Enter the number of elements to calculate average::\n");
scanf("%d", &x);
printf("Enter %d elements one by one\n\n", x);
for(i = 0; i < x; i++) {
scanf("%f", &y);
avg += y;
}
avg /= x;
printf("\nThe average of the entered input numbers is = %f", avg);
return 0;
}
Output
Enter the number of elements to calculate average::
4
Enter 4 elements one by one
646
642
656
435
The average of the entered input numbers is = 594.750000
C program to find average of N numbers using While loop
// C program to find average of N numbers using While loop
#include <stdio.h>
int main() {
int x, i = 0;
float avg = 0, y;
// x - To store number of elements
// avg - To store total average value
// y - To store total input numbers
printf("Enter the number of elements to calculate average::\n");
scanf("%d", &x);
printf("Enter %d elements one by one\n\n", x);
while (i < x) {
scanf("%f", &y);
avg += y;
i++;
}
avg /= x;
printf("\nThe average of the entered input numbers is = %f", avg);
return 0;
}
Explanation
In this given program, we have taken inputs 4
size of incoming input numbers 646
, 642
, 656
, and 435
. Now we applied the standard formula to calculate the average of these numbers.
(646 + 642 + 656 + 435) / 4 = 594.750000
Then it will return the 594.750000
average of these numbers in the above calculation.
C program to find average of N numbers using Do While loop
// C program to find average of N numbers using Do While loop
#include <stdio.h>
int main() {
int x, i = 0;
float avg = 0, y;
// x - To store number of elements
// avg - To store total average value
// y - To store total input numbers
printf("Enter the number of elements to calculate average::\n");
scanf("%d", &x);
printf("Enter %d elements one by one\n\n", x);
do {
scanf("%f", &y);
avg += y;
i++;
} while (i < x);
avg /= x;
printf("\nThe average of the entered input numbers is = %f", avg);
return 0;
}
C program to find average of N numbers using Array
// C program to find average of N numbers using Array
#include <stdio.h>
int main() {
int x, i = 0;
float avg = 0, y;
// x - To store number of elements
// avg - To store total average value
// y - To store total input numbers
printf("Enter the number of elements to calculate average::\n");
scanf("%d", &x);
float numbers[x];
printf("\nEnter %d elements one by one::\n", x);
for (i = 0; i < x; i++) {
scanf("%f", &numbers[i]);
avg += numbers[i];
}
avg /= x;
printf("\nThe average of the entered input numbers is: %f\n", avg);
return 0;
}
Output
Enter the number of elements to calculate average::
3
Enter 3 elements one by one::
23
45
87
The average of the entered input numbers is: 51.666668
C program to find average of N numbers using Functions
// C program to find average of N numbers using Functions
#include <stdio.h>
// This function will find the average of N numbers
void FindAverage(int size) {
float avg = 0, y; // To store the total average value
printf("\nEnter %d elements one by one::\n", size);
for(int i = 0; i < size; i++) {
scanf("%f", &y);
avg += y;
}
avg /= size;
printf("\nThe average of the entered input numbers is = %f\n", avg);
}
// It's the driver function
int main() {
int x; // To store the number of elements
printf("Enter the number of elements to calculate average::\n");
scanf("%d", &x);
FindAverage(x);
return 0;
}
C program to find average of N numbers using Array and Function
// C program to find average of N numbers using Array and Function
#include <stdio.h>
// This function will find the average of N numbers
void FindAverage(int size) {
float avg = 0; // To store the total average value
float numbers[size]; // To store the values into Array
printf("\nEnter %d elements one by one::\n", size);
for(int i = 0; i < size; i++) {
scanf("%f", &numbers[i]);
avg += numbers[i];
}
avg /= size;
printf("\nThe average of the entered input numbers is = %f\n", avg);
}
// It's the driver function
int main() {
int x; // To store the number of elements
printf("Enter the number of elements to calculate average::\n");
scanf("%d", &x);
FindAverage(x);
return 0;
}
Enter the number of elements to calculate average::
5
Enter 5 elements one by one::
23
54
56
98
12
The average of the entered input numbers is = 48.599998
Also, visit these links
C++ Program to Find the Average of N Numbers