Write a C program to find the sum and average of N numbers.
int main() {
float data[100], sum = 0.0, average;
int n, i;
/* get the no of entries from the user */
printf("Enter the no of elements:");
scanf("%d", &n);
/* get the input entries */
printf("Enter your inputs:\n");
/* find the sum of N numbers */
for (i = 0; i < n; i++) {
scanf("%f", &data[i]);
sum = sum + data[i];
}
/* calculate the average */
average = sum / n;
/* print the outputs - sum, average */
printf("Sum: %.3f\n", sum);
printf("Average: %.3f\n", average);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the no of elements:5
Enter your inputs:
150
300
450
200
100
Sum: 1200.000
Average: 240.000
Enter the no of elements:5
Enter your inputs:
150
300
450
200
100
Sum: 1200.000
Average: 240.000
No comments:
Post a Comment