Java program to calculate mean of given numbers


Mean is an average value of given set of numbers. It is calculated similarly to that of the average value. Adding all given number together and then dividing them by the total number of values produces mean.

For Example 

Mean of 3, 5, 2, 7, 3 is (3 + 5 + 2 + 7 + 3) / 5 = 4

Algorithm

  • Take an integer set A of n values.
  • Add all values of A together.
  • Divide result of Step 2 by n.
  • The result is mean of A's values.

Program

public class CaculatingMean {
   public static void main(String args[]){
      float mean;
      int sum, i;
      int n = 5;
      int a[] = {2,6,7,4,9};
      sum = 0;

      for(i = 0; i < n; i++) {
         sum+=a[i];
      }
      System.out.println("Mean ::"+ sum/(float)n);
   }
}

Output

Mean::5.6

Updated on: 19-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements