Average of Numbers in an Array using Loops in C
To calculate the average of numbers in an array using loops in C, we need to iterate through the array, sum up all elements, and then divide the sum by the total number of elements. We can achieve this using a for
, while
, or do-while
loop.
Examples to Calculate the Average
1. Calculating the Average using a for
Loop
In this example, we will declare an array of integers, iterate through it using a for
loop to compute the sum, and then divide by the number of elements to find the average.
main.c
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);
int sum = 0;
float average;
// Loop through the array using for loop
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
average = (float) sum / size;
printf("Average: %.2f\n", average);
return 0;
}
Explanation:
- Declare an integer array
numbers[]
with five elements. - Determine the number of elements using
sizeof(numbers) / sizeof(numbers[0])
. - Initialize
sum
to 0, which will store the total sum of elements. - Use a
for
loop to iterate through the array and add each element tosum
. - Compute the average by dividing
sum
bysize
and store it inaverage
. - Use
printf()
to print the average with two decimal places.
Output:
Average: 30.00
2. Calculating the Average using a while
Loop
In this example, we will use a while
loop instead of a for
loop to iterate through the array and compute the sum before finding the average.
main.c
#include <stdio.h>
int main() {
int numbers[] = {5, 15, 25, 35, 45};
int size = sizeof(numbers) / sizeof(numbers[0]);
int sum = 0, i = 0;
float average;
// Loop through the array using while loop
while (i < size) {
sum += numbers[i];
i++;
}
average = (float) sum / size;
printf("Average: %.2f\n", average);
return 0;
}
Explanation:
- Declare an integer array
numbers[]
with five elements. - Determine the number of elements using
sizeof(numbers) / sizeof(numbers[0])
. - Initialize
sum
to 0 andi
to 0. - Use a
while
loop to iterate through the array untili < size
. - Add each element to
sum
inside the loop and incrementi
. - Compute the average and print it with two decimal places.
Output:
Average: 25.00
3. Calculating the Average using a do-while
Loop
In this example, we will use a do-while
loop, ensuring at least one iteration even if the array is empty.
main.c
#include <stdio.h>
int main() {
int numbers[] = {3, 6, 9, 12, 15};
int size = sizeof(numbers) / sizeof(numbers[0]);
int sum = 0, i = 0;
float average;
// Loop through the array using do-while loop
do {
sum += numbers[i];
i++;
} while (i < size);
average = (float) sum / size;
printf("Average: %.2f\n", average);
return 0;
}
Explanation:
- Declare an integer array
numbers[]
with five elements. - Determine the number of elements using
sizeof(numbers) / sizeof(numbers[0])
. - Initialize
sum
to 0 andi
to 0. - Use a
do-while
loop, which runs at least once. - Add each element to
sum
and incrementi
in each iteration. - Compute the average and print it with two decimal places.
Output:
Average: 9.00
Conclusion
In this tutorial, we learned how to calculate the average of numbers in an array using different loops in C:
- Using a
for
loop for known iterations. - Using a
while
loop when iteration depends on a condition. - Using a
do-while
loop to ensure at least one execution.