Algorithm


1. Input:
   - Take an array as input.
2. Initialize Variables:

   - Set a variable sum to 0 to store the sum of all elements.
   - Set a variable count to 0 to keep track of the number of elements.
3. Loop through the Array:

   - Use a loop to iterate through each element in the array.
   - For each element:
    - Add the element to the sum.
    - Increment the count by 1.
4. Calculate Average:

     - Divide the sum by the count to get the average.
5. Output:

   - Display or return the calculated average.

Code Examples

#1 Code Example- take array of n elements and find the average of the array

Code - C Programming

#include<stdio.h>
int main() {
    int arr[10] = {2,3,4,54,65,34,23,34,123,80};
    
    int sum=0, average;
    
    for(int i=0;i<10;i++){
        sum += arr[i];
    }
    average = sum/10;
    printf("The average of given numbers : %d", average);

    return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
The average of given numbers : 42

#2 Code Example- C Programing Problem Solve by taking number of elements and elements from use

Code - C Programming

#include<stdio.h>

int main() {
    
    int arr[100], n,sum =0;
    
    printf("Enter number of elements you want to find average  :");
    scanf("%d", &n);
    for(int i=0;i < n;i++){
        printf("Enter single element %d:", i+1);
        scanf("%d", &arr[i]);
    }
    float average;
    
    for(int i=0;i < n;i++){
        sum += arr[i];
    }
    average = sum/n;
    printf("The average of elements is : %f", average);

    return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter number of elements you want to find average :
6
Enter single element 1:4
Enter single element 2:65
Enter single element 3:34
Enter single element 4:44
Enter single element 5:22
Enter single element 6:12
The average of elements is : 30.000000
Advertisements

Demonstration


C Programing Example to Find Average using Array-DevsEnv

Next
Appending into a File in C Programming