Write a c program to find array elements greater than average using for loop. In this c example, first, we find the sum and average of array elements. Next, we used the if statement (if(arr[i] > arrAvg)) checks whether each array element is greater than average. If True, it will print that element.
#include <stdio.h> int main() { int Size, i, arrSum = 0; float arrAvg = 0; printf("Please Enter the Array size = "); scanf("%d", &Size); int arr[Size]; printf("Enter the Array Items = "); for (i = 0; i < Size; i++) { scanf("%d", &arr[i]); arrSum = arrSum + arr[i]; } arrAvg = (float)arrSum / Size; printf("\nThe Sum of Array Items = %d\n", arrSum); printf("The Average of Array Items = %.2f\n", arrAvg); printf("\nThe Array Items Greater Than The Average = "); for (i = 0; i < Size; i++) { if(arr[i] > arrAvg) { printf("%d ", arr[i]); } } printf("\n"); }
This c program uses a while loop to calculate the average of array elements and prints the items greater than average.
#include <stdio.h> int main() { int Size, i, arrSum = 0; float arrAvg = 0; printf("Please Enter the size = "); scanf("%d", &Size); int arr[Size]; printf("Enter the Items = "); i = 0; while(i < Size) { scanf("%d", &arr[i]); arrSum = arrSum + arr[i]; i++; } arrAvg = (float)arrSum / Size; printf("\nThe Sum = %d\n", arrSum); printf("The Average = %.2f\n", arrAvg); printf("\nThe Array Items Greater Than The Average\n"); i = 0; while(i < Size) { if(arr[i] > arrAvg) { printf("%d ", arr[i]); } i++; } printf("\n"); }
Please Enter the size = 8
Enter the Items = 22 33 98 67 12 19 55 7
The Sum = 313
The Average = 39.12
The Array Items Greater Than The Average
98 67 55