Explanation:
We will solve this problem using array. Because, without array we need to take user input for n numbers again and again by using printf () and scanf () functions for n times each. But in case of array, just we need to initialize the array size and run a for loop from 0 to n-1.
- Formula to find the average of n numbers is : sum of n numbers / n;
In this c program we will first initialize float sum = 0; and for each array elements we will add array[i] with sum till n-1.
CODE STARTS HERE :
- #include<stdio.h>
- int main()
- {
- int n;
- printf("For how many numbers you want average?\n");
- scanf("%d",&n);
- int nums[n];
- float sum=0;
- printf("Enter the numbers : \n");
- for(int i=1;i<=n;i++)
- {
- printf("Enter %d number: \n",i);
- scanf("%d",&nums[i]);
- sum=sum+nums[i];
- }
- float avg=sum/(float)n;
- printf("The average is: %f\n",avg);
- return 0;
- }
INPUTS AND OUTPUTS:
For how many numbers you want average?
4
Enter the numbers :
Enter 1 number:
11
Enter 2 number:
12
Enter 3 number:
12
Enter 4 number:
22
The average is: 14.250000