Write a program to Calculate the Average of array elements enter by users.
C Program
#include <stdio.h>int main(){int n;printf("Number of elements: ");scanf("%d", &n);float arr[n];printf("Enter array elements:\n");for (int i = 0; i < n; i++){scanf("%f", &arr[i]);}float sum = 0;for (int i = 0; i < n; i++){sum = sum + arr[i];}float average = sum / n;printf("Average of array numbers is %f", average);return 0;}
Input:
Number of elements: 4
Enter array elements:
12.4
5.3
34.6
23.5
Output:
Average of array numbers is 18.950001
No comments:
Post a Comment