Average of n numbers Example Java Program


Definition

An average is the sum of a list of numbers divided by the number of numbers in the list. In mathematics and statistics, this would be called the arithmetic mean.

Average of n numbers Example Program

import java.util.Scanner;
public class Average{
	public static void main(String[] args) {
		Scanner in= new Scanner(System.in); 
		System.out.print("Enter N number of elements " +"for finding average : ");
		final int count= in.nextInt();;
		System.out.println("");
		double[] numbers = new double[count];
		double sum = 0;
		for (int i = 0; i < count; i++) {
		System.out.print("Enter number "+(i+1)+" : " );
		numbers[i] = in.nextDouble();
		sum += numbers[i];
		}
    double avg = sum / count;
    System.out.println("");
    System.out.println("Average of the given numbers is " + avg);
	}
}

Sample Output

Output is:
Enter N number of elements for finding average : 5

Enter number 1 : 45
Enter number 2 : 567
Enter number 3 : 7890
Enter number 4 : 345
Enter number 5 : 567

Average of the given numbers is 1882.8