Ask Question
21 January, 03:02

Create a Java static method named getAverageOfFours that takes an array of integers as a parameter and returns the average of the values in the array that are evenly divisible by 4. Your method must work for an int array of any size. Example: If the array contains [10, 48, 16, 99, 84, 85], the method would return 49.33333.

+5
Answers (1)
  1. 21 January, 03:28
    0
    public class average{

    public static float getAverageOfFours (int[] arr) {

    int n = arr. length;

    float sum = 0;

    int count=0;

    for (int i=0; i
    if (arr[i]%4==0) {

    sum = sum+arr[i];

    count++;

    }

    }

    float avg = sum/count;

    return avg;

    }

    public static void main (String []args) {

    int[] array = {10, 48, 16, 99, 84, 85};

    float result = getAverageOfFours (array);

    System. out. println ("The average is "+result);

    }

    }

    Explanation:

    Create the function with one parameter and return type is float.

    Declare the variable and store the size of array. Take a for loop for traversing in the array and and check the element is divisible by 4 or not by using if statement. If condition true take the sum and increment the count.

    This process continue until the condition is true.

    finally take the average and return it.

    Create the main function for calling the function with pass the array as argument and also define the array.

    After that print the result return by the function.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Create a Java static method named getAverageOfFours that takes an array of integers as a parameter and returns the average of the values in ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers