Answer:
public class average{
public static float getAverageOfFours(int[] arr){
int n = arr.length;
float sum = 0;
int count=0;
for(int i=0;i<n;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);
}
}
Step-by-step 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.