Probability of getting K heads in N coin tosses | Java program to find probability of getting K heads in N coin tosses ; function to calculate factorial ; apply the formula ; Driver function ; call count_heads with n and r
class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static double count_heads ( int n , int r ) { double output ; output = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; output = output / ( Math . pow ( 2 , n ) ) ; return output ; } public static void main ( String [ ] args ) { int n = 4 , r = 3 ; System . out . print ( count_heads ( n , r ) ) ; } }