Related Articles
Probability of getting K heads in N coin tosses
• Difficulty Level : Easy
• Last Updated : 07 Apr, 2021

Given two integers N and R. The task is to calculate the probability of getting exactly r heads in n successive tosses.
A fair coin has an equal probability of landing a head or a tail on each toss.

Examples:

Input : N = 1, R = 1
Output : 0.500000

Input : N = 4, R = 3
Output : 0.250000 

Approach
Probability of getting K heads in N coin tosses can be calculated using below formula: Below is the implementation of the above approach:

## C++

 #include using namespace std; // function to calculate factorialint fact(int n){    int res = 1;    for (int i = 2; i <= n; i++)        res = res * i;    return res;} // apply the formuladouble count_heads(int n, int r){    double output;    output = fact(n) / (fact(r) * fact(n - r));    output = output / (pow(2, n));    return output;} // Driver functionint main(){    int n = 4, r = 3;         // call count_heads with n and r    cout << count_heads(n, r);    return 0;}

## Java

 class GFG{  // function to calculate factorialstatic int fact(int n){    int res = 1;    for (int i = 2; i <= n; i++)        res = res * i;    return res;}  // apply the formulastatic 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;}  // Driver functionpublic static void main(String[] args){    int n = 4, r = 3;          // call count_heads with n and r    System.out.print(count_heads(n, r));}} // This code is contributed by 29AjayKumar

## Python3

 # Python3 program to find probability# of getting K heads in N coin tosses # Function to calculate factorialdef fact(n):         res = 1    for i in range(2, n + 1):        res = res * i    return res # Applying the formuladef count_heads(n, r):         output = fact(n) / (fact(r) * fact(n - r))    output = output / (pow(2, n))    return output # Driver coden = 4r = 3 # Call count_heads with n and rprint (count_heads(n, r)) # This code is contributed by Pratik Basu

## C#

 using System; public class GFG{   // Function to calculate factorialstatic int fact(int n){    int res = 1;    for (int i = 2; i <= n; i++)        res = res * i;    return res;}   // Apply the formulastatic 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;}   // Driver functionpublic static void Main(String[] args){    int n = 4, r = 3;           // Call count_heads with n and r    Console.Write(count_heads(n, r));}}// This code contributed by sapnasingh4991

## Javascript

 
Output:
0.250000

Time Complexity: In this implementation, we have to calculate factorial based on the value n, so time complexity would be O(n)
Auxiliary Space: In this implementation, we are not using any extra space, so auxiliary space required is O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up