- Write 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; /** * Java Program to find mean of N numbers */ 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**