Description
To find the average of elements present in the array, Calculate the sum of elements of array, divide the sum by total number of elements present in the array. To get the appropriate result, use typecasting.
C/C++
/*C program to find average of elements*/ // Save it as AverageOfElements.c #include<stdio.h> int main(){ int i,n; printf("Enter the size of array : "); scanf("%d",&n); float arr[n]; printf("Enter the elements of array : "); for(i=0;i<n;i++){ scanf("%f",&arr[i]); } float sum = 0.0; for(i=0;i<n;i++) { sum += arr[i]; } printf("Sum of elements : %.2f", sum); float average = sum/n; printf("\nThe average of elements : %.2f", average); return 0; }
Java
/*Java program to find average of elements*/ // Save it as AverageOfElements.java import java.io.*; import java.util.Scanner; public class AverageOfElements { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the size of array : "); int n = scanner.nextInt(); int arr[] = new int[n]; System.out.println("Enter the elements of the array : "); for(int i=0;i<n;i++) { arr[i] = scanner.nextInt(); } int sum = 0; for(int i=0;i<n;i++) { sum += arr[i]; } float average = (float)sum/n; System.out.println("The sum of elements : " + sum); System.out.println("The Average of elements : " + average); } }
Related Programs
1) Find Smallest element and it’s position of an Array2) Program for finding Second Largest Element Of Array
3) Program to form a number using entered digits
4) Program to merge two sorted arrays
5) Program to swap maximum and minimum element of Array
6) Program to Search for an Element in an Array
7) Program to Rearrange an array such that arr[i]=i
8) Program to Find Median of a Array
9) Program to Find Mode of a Array
10) Program to Find the smallest missing number