Write a program to find the sum and average of n numbers of the array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,sum=0;
float average;
clrscr();
printf("How many elements? ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
sum+=a[i]; // shorthand assignment operator which is
sum=sum+a[i]
average=(float)sum/n; // explicit type conversion
printf("Sum=%d\n",sum);
printf("Average=%f",average);
getch();
}
Output:
How many elements? 3
Enter the elements
1
2
3
Sum=6
No comments: