Java Program to Calculate Arithmetic Mean of N Numbers

Here is a java program to find average of N numbers using for loop. Given a set of N numbers, we have to calculate and print the arithmetic mean of given N numbers. Arithmetic mean is also known as average.

For Example,
Input Numbers : 12 7 9 10 3 15 16 2
Arithmetic Mean : 9.25

In this java program, we first ask user to enter the number of elements as store it in an integer variable "count". Then we take "count" numbers as input from user using for loop and add them in variable "sum". At the end of for loop, "sum" will contain the total sum of all input numbers. Now, to calculate arithmetic mean we will divide and "sum" by "count".


Java program to calculate the average of N numbers

package com.tcc.java.programs;

import java.util.Scanner;

public class MeanNumber {
    public static void main(String[] args) {
        int count, i;
        float sum = 0, mean;
        Scanner scanner;
        scanner = new Scanner(System.in);

        System.out.println("Enter Number of Elements");
        count = scanner.nextInt();

        System.out.println("Enter " + count + " 
            Elements");
        for (i = 0; i < count; i++) {
            sum += scanner.nextInt();
        }
        // Mean or Average = Sum/Count
        mean = sum / count;

        System.out.println("Mean : " + mean);
    }
}
Output
Enter Number of Elements
6
Enter 6 Elements
10 9 15 20 30 22
Mean : 17.666666

Recommended Posts
Java Program to Calculate Average and Percentage Marks
Java Program to Find Sum of all Odd Numbers between 0 to N
Java Program to Find LCM and GCD of Two Numbers
Java Program to Find All Factors of a Number
Java Program to Print Prime Numbers between 1 to 100
Java Program to Find Sum of Digits of a Number
Java Program to Make a Simple Calculator using Switch Statement
Java Program to Calculate Simple Interest
Java Program to Find Sum of Elements of an Array
Java Program to calculate area and circumference of circle
Java Program to Add Two Numbers
All Java Programs