C Program to Find Average of Array Elements
Write C Program to Find Average of Array Elements
// C Program to Find Average of Array Elements #include <stdio.h> int main() { int A[] = {1, 2, 3, 4, 5, 6, 7, 8}, i; int n = sizeof(A) / sizeof(A[0]); printf("Array Elements :--> "); for(i=0; i<n; i++) printf("%d ", A[i]); float sum = 0; for(i = 0; i < n; i++) { sum += A[i]; } printf("\n\nNumber of Array Elements :--> %d", n); printf("\nSum of Array Elements :--> %.0f", sum); printf("\nAverage of Array Elements :--> %.2f", (sum/n)); return 0; }
Output:
Array Elements :--> 1 2 3 4 5 6 7 8
Number of Array Elements :--> 8
Sum of Array Elements :--> 36
Average of Array Elements :--> 4.50