Geometric mean ( Two Methods ) | Java Program to calculate the geometric mean of the given array elements . ; function to calculate geometric mean and return float value . ; declare sum variable and initialize it to 1. ; Compute the sum of all the elements in the array . ; compute geometric mean through formula antilog ( ( ( log ( 1 ) + log ( 2 ) + . . . + log ( n ) ) / n ) and return the value to main function . ; Driver function ; function call
import java . io . * ; class GFG { static float geometricMean ( int [ ] arr , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( float ) Math . log ( arr [ i ] ) ; sum = sum / n ; return ( float ) Math . exp ( sum ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; int n = arr . length ; System . out . println ( geometricMean ( arr , n ) ) ; } }