Java Program to Calculate Arithmetic Mean

This article covers a program in Java that find and prints arithmetic mean of n numbers, entered by user. Arithmetic mean of n numbers can be calculated as:

Arithmetic Mean = (Sum of All Numbers)/(n)

Calculate Arithmetic Mean or Average in Java

The question is, write a Java program to find and print the arithmetic mean of n numbers, entered by user at run-time of the program. The program given below is its answer:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int n, i, sum=0;
      float armean;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("How many numbers to enter ? ");
      n = scan.nextInt();
      int[] arr = new int[n];
      
      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.println("\nArithmetic Mean = " +armean);
   }
}

The snapshot given below shows the sample run of above program with user input 6 as size and 10, 20, 30, 40, 50, 60 as six numbers to find and print arithmetic mean of these six numbers:

java program calculate arithmetic mean

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »