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 = 1Output :0.500000Input :N = 4, R = 3Output :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 <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate factorial ` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `res = res * i; ` ` ` `return` `res; ` `} ` ` ` `// apply the formula ` `double` `count_heads(` `int` `n, ` `int` `r) ` `{ ` ` ` `double` `output; ` ` ` `output = fact(n) / (fact(r) * fact(n - r)); ` ` ` `output = output / (` `pow` `(2, n)); ` ` ` `return` `output; ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `int` `n = 4, r = 3; ` ` ` ` ` `// call count_heads with n and r ` ` ` `cout << count_heads(n, r); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`class` `GFG{ ` ` ` `// function to calculate factorial ` `static` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = ` `1` `; ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `res = res * i; ` ` ` `return` `res; ` `} ` ` ` `// apply the formula ` `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; ` `} ` ` ` `// Driver function ` `public` `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 ` |

*chevron_right*

*filter_none*

## Python3

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

*chevron_right*

*filter_none*

## C#

`using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `// Function to calculate factorial ` `static` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `res = res * i; ` ` ` `return` `res; ` `} ` ` ` `// Apply the formula ` `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; ` `} ` ` ` `// Driver function ` `public` `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 ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Probability of not getting two consecutive heads together in N tosses of coin
- Probability of getting at least K heads in N tosses of Coins
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Probability of getting more heads than tails when N biased coins are tossed
- Expected number of coin flips to get two heads in a row?
- Count of total Heads and Tails after N flips in a coin
- Make a fair coin from a biased coin
- Expected Number of Trials to get N Consecutive Heads
- Probability of getting a sum on throwing 2 Dices N times
- Probability of getting more value in third dice throw
- Probability of getting a perfect square when a random number is chosen in a given range
- Probability of getting all possible values on throwing N dices
- Coin Change | DP-7
- Frobenius coin problem
- Coin Change | BFS Approach
- Find the player who will win the Coin game
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Philaland Coin | TCS Mockvita 2020
- Count ways to distribute exactly one coin to each worker
- Generate integer from 1 to 7 with equal probability

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.