16 May 2024

Find The Average of Array

FIND THE AVERAGE OF ARRAY 

What is the Average?

The average (or mean) of a dataset is calculated by dividing the sum of all elements by the number of elements. It offers a quick way to understand the overall trend or distribution of values.

Formula:

Average=Sum of ElementsNumber of Elements\text{Average} = \frac{\text{Sum of Elements}}{\text{Number of Elements}}

Steps to Calculate the Average

  1. Input: Start with an array of numbers.
  2. Sum: Add up all the elements in the array.
  3. Divide: Divide the sum by the total number of elements.

Example:

For the array [10,20,30,40,50][10, 20, 30, 40, 50]

  • Sum = 10+20+30+40+50=150
  • Number of elements = 55
  • Average = 150/5=30150 / 5 = 30
CODE

import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

        int sum=0;

        int n=sc.nextInt();

        for(int i=0;i<n;i++){

            sum+=sc.nextInt();

        }

        System.out.print(sum/n);

    }

}

No comments:

Post a Comment