N. Achyut java,Java Introductions Write a static method to calculate the average of a one dimensional array in java

Write a static method to calculate the average of a one dimensional array in java

static method to calculate the average of a one dimensional array

Write a static method to calculate the average of a one dimensional array ?

static class belongs to the class rather than instance of class.

Java Program for static method to calculate the average of a one dimensional array

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assignments;

/**
 *
 * @author Achyut1324
 */
public class Assignment7 {
    static int[] array = {2,4,5,6,7,8};
    public static void main(String[] args) {
        System.out.println("The average of abouve array is:"+average(array));
    }
    public static float average(int[] args){
        float sum =0;
        for(int i =0; i<array.length;i++){
            sum = sum+ array[i];
        } 
        float x = sum/ array.length;
        return x;
    }
    
}

Output of the Program :

run:
The average of abouve array is:5.3333335
BUILD SUCCESSFUL (total time: 1 second)

Related Post