C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Average of array elements

C Program: Average of array elements

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i;

    float sum = 0, average;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    for(i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Calculate sum of elements

    for(i = 0; i < n; i++) {

        sum += arr[i];

    }

 

    // Calculate average

    average = sum / n;

 

    // Display result

    printf("\nAverage of array elements = %.2f\n", average);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 5
Enter 5 elements:
10 20 30 40 50

OUTPUT :
Average of array elements = 30.00

Explanation

  1. The user enters how many elements (n) they want in the array.
  2. A for loop takes input of all array elements.
  3. Another for loop calculates the sum of all elements.
  4. The average is found by dividing sum by n.
  5. The result is displayed with two decimal places using %.2f.
  6. The average of numbers is calculated as:

 C Programs - Arrays

  1. Floating-point arithmetic (float) is used to get decimal precision.

 

C Program: Average of array elements

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i = 0;

    float sum = 0, average;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    while(i < n) {

        scanf("%d", &arr[i]);

        i++;

    }

 

    // Calculate sum using while loop

    i = 0;

    while(i < n) {

        sum += arr[i];

        i++;

    }

 

    // Calculate average

    average = sum / n;

 

    // Display result

    printf("\nAverage of array elements = %.2f\n", average);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 4
Enter 4 elements:
5 10 15 20

OUTPUT :
Average of array elements = 12.50

Explanation

  1. The program first asks the user for the total number of array elements (n).
  2. A while loop is used to input all the numbers into the array.
  3. Another while loop calculates the total sum of these elements.
  4. The average is then calculated using the formula:

                       Average = Sum / n

  1. Finally, the average is displayed with two decimal precision using %.2f.
  1. The while loop runs until i < n, allowing controlled input and processing.
  2. Using float ensures that the average value includes decimals.
  3. %.2f in printf ensures the output shows two digits after the decimal point.

 

C Program: Average of array elements

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i = 0;

    float sum = 0, average;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements using do...while loop

    printf("Enter %d elements:\n", n);

    do {

        scanf("%d", &arr[i]);

        i++;

    } while(i < n);

 

    // Calculate sum using do...while loop

    i = 0;

    do {

        sum += arr[i];

        i++;

    } while(i < n);

 

    // Calculate average

    average = sum / n;

 

    // Display result

    printf("\nAverage of array elements = %.2f\n", average);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 5
Enter 5 elements:
2 4 6 8 10

OUTPUT :
Average of array elements = 6.00

Explanation

  1. The program starts by asking the user for the total number of elements (n).
  2. The do...while loop is used to take input — ensuring that the loop runs at least once, even if n = 1.
  3. Another do...while loop computes the total sum of all elements.
  4. The average is calculated as:

                             Average = Sum / n

  1. Finally, the result is displayed with two decimal places for clarity.
  1. The do..while loop is useful when at least one iteration must occur, such as reading user input.
  2. Floating-point arithmetic is used to handle non-integer averages.
  3. The format specifier %.2f ensures the result is displayed neatly up to two decimal places.