You are on page 1of 2

Calculate Arithmetic Mean or Average in Java

To calculate the arithmetic mean of some numbers in Java programming, you have to ask


to the user to enter number size then ask to enter the numbers of that size to perform the
addition, then make a variableresponsible for the average and place addition/size in
average, then display the result on the output screen.

Java Programming Code to Calculate Arithmetic Mean or Average

Following Java Program ask to the user to enter "How many number he want to enter", then
ask to enter all the numbers to calculate and display the arithmetic mean:

/* Java Program Example - Calculate Arithmetic Mean */

import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int n, i, sum=0, armean;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);

System.out.print("How many Number you want to Enter ? ");


n = scan.nextInt();

System.out.print("Enter " +n+ " Numbers : ");


for(i=0; i<n; i++)
{
arr[i] = scan.nextInt();
sum = sum + arr[i];
}

armean = sum/n;

System.out.print("Arithmetic Mean = " +armean);


}
}

When the above Java Program is compile and executed, it will produce the following output:

You might also like