Total Average and Numbers Greater Than Average in Array

Find total, average, and numbers greater than average in array | Here we will write a program to find the total sum and average of array elements. After that also find numbers greater than average in the array.

Program description:- Write a program to find total and average of n given numbers and also find the numbers which are greater than average.

C Program to Find Total, Average, & Numbers Greater than Average in Array

#include<stdio.h>
int main()
{
   int a[100], n, i, sum=0;
   float avg;

   printf("Enter array size [1-100]: ");
   scanf("%d",&n);
   printf("Enter %d elements: ", n);

   // finding sum and average
   for(i=0; i<n; i++)
   {
     scanf("%d",&a[i]);
     sum += a[i];
   }

   avg = (float)sum/n;
   printf("Sum=%d \t Average=%.2f", sum, avg);

   // finding numbers greater than average
   printf("\nNumbers greater than average are:\n");
   for(i=0; i<n; i++)
   {
     if(a[i]>avg) printf("%d\t",a[i]);
   }

   return 0;
}

Output for the different test-cases:-

Enter array size [1-100]: 5
Enter 5 elements: 10 20 5 15 30
Sum=80 Average=16.00
Numbers greater than average are:
20 30

Enter array size [1-100]: 3
Enter 3 elements: 1 2 8
Sum=11 Average=3.67
Numbers greater than average are:
8

The variable sum is initialized with 0, and calculated as sum+=array element. The variable avg (average) is declared as a float because it can be floating data type. The average is calculated as the sum of all elements divided by the number of array elements. Finally, using if condition all elements greater than average will be displayed.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Similar C programming examples on Arrays

Leave a Comment

Your email address will not be published. Required fields are marked *