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

</>
Copy
#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:

  1. Declare an integer array numbers[] with five elements.
  2. Determine the number of elements using sizeof(numbers) / sizeof(numbers[0]).
  3. Initialize sum to 0, which will store the total sum of elements.
  4. Use a for loop to iterate through the array and add each element to sum.
  5. Compute the average by dividing sum by size and store it in average.
  6. 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

</>
Copy
#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:

  1. Declare an integer array numbers[] with five elements.
  2. Determine the number of elements using sizeof(numbers) / sizeof(numbers[0]).
  3. Initialize sum to 0 and i to 0.
  4. Use a while loop to iterate through the array until i < size.
  5. Add each element to sum inside the loop and increment i.
  6. 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

</>
Copy
#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:

  1. Declare an integer array numbers[] with five elements.
  2. Determine the number of elements using sizeof(numbers) / sizeof(numbers[0]).
  3. Initialize sum to 0 and i to 0.
  4. Use a do-while loop, which runs at least once.
  5. Add each element to sum and increment i in each iteration.
  6. 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:

  1. Using a for loop for known iterations.
  2. Using a while loop when iteration depends on a condition.
  3. Using a do-while loop to ensure at least one execution.