Probability of getting K heads in N coin tosses | Function to calculate factorial ; Apply the formula ; Driver Code ; Call count_heads with n and r
function fact ( n ) { var res = 1 ; for ( var i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function count_heads ( n , r ) { var output ; output = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; output = output / ( Math . pow ( 2 , n ) ) ; return output ; } var n = 4 , r = 3 ; document . write ( count_heads ( n , r ) ) ;