Given a fair coin which is tossed **N** times, the task is to determine the probability such that no two heads occur consecutively.

**Examples:**

Input:N = 2

Output:0.75

Explanation:

When the coin is tossed 2 times, the possible outcomes are {TH, HT, TT, HH}.

Since in 3 out of 4 outcomes, heads don’t occur together.

Therefore, the required probability is (3/4) or 0.75

Input:N = 3

Output:0.62

Explanation:

When the coin is tossed 3 times, the possible outcomes are {TTT, HTT, THT, TTH, HHT, HTH, THH, HHH}.

Since in 5 out of 8 outcomes, heads don’t occur together.

Therefore, the required probability is (5/8) or 0.62

**Approach:** The following observation on the number of favourable outcomes has to be made.

**When N = 1:**The possible outcomes are {T, H}. There are**two**favourable outcomes out of two.**When N = 2:**The possible outcomes are {TH, HT, TT, HH}. There are**three**favourable outcomes out of four.**When N = 3:**Similarly, the possible outcomes are {TTT, HTT, THT, TTH, HHT, HTH, THH, HHH}. There are**five**favourable outcomes out of eight.**When N = 4:**Similarly, the possible outcomes are {TTTT, TTTH, TTHT, THTT, HTTT, TTHH, THTH, HTHT, HHTT, THHT, HTTH, THHH, HTHH, HHTH, HHHT, HHHH}. There are**eight**favourable outcomes out of sixteen.

Clearly, the number of favourable outcomes follow a Fibonacci series where Fn(1) = 2, Fn(2) = 3 and so on. Therefore, the idea is to implement the Fibonacci sequence in order to find the number of favourable cases. Clearly, the total number of cases are 2^{N}.

To calculate the probability, the following formula is used:

P = favourable cases / Total number of cases

Below is the implementation of the above approach:

`# Python implementation to find the ` `# probability of not getting two ` `# consecutive heads together when ` `# N coins are tossed ` ` ` ` ` `import` `math ` ` ` `# Function to compute the N-th ` `# Fibonacci number in the ` `# sequence where a = 2 ` `# and b = 3 ` `def` `probability(N): ` ` ` ` ` `# The first two numbers in ` ` ` `# the sequence are initialized ` ` ` `a ` `=` `2` ` ` `b ` `=` `3` ` ` ` ` `# Base cases ` ` ` `if` `N ` `=` `=` `1` `: ` ` ` `return` `a ` ` ` `elif` `N ` `=` `=` `2` `: ` ` ` `return` `b ` ` ` `else` `: ` ` ` ` ` `# Loop to compute the fibonacci ` ` ` `# sequence based on the first ` ` ` `# two initialized numbers ` ` ` `for` `i ` `in` `range` `(` `3` `, N ` `+` `1` `): ` ` ` `c ` `=` `a ` `+` `b ` ` ` `a ` `=` `b ` ` ` `b ` `=` `c ` ` ` `return` `b ` ` ` `# Function to find the probability ` `# of not getting two consecutive ` `# heads when N coins are tossed ` `def` `operations(N): ` ` ` ` ` `# Computing the number of ` ` ` `# favourable cases ` ` ` `x ` `=` `probability (N) ` ` ` ` ` `# Computing the number of ` ` ` `# all possible outcomes for ` ` ` `# N tosses ` ` ` `y ` `=` `math.` `pow` `(` `2` `, N) ` ` ` ` ` `return` `round` `(x ` `/` `y, ` `2` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `10` ` ` ` ` `print` `(operations(N)) ` ` ` |

*chevron_right*

*filter_none*

**Output:**

0.14

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 getting K heads in N coin tosses
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Probability of getting at least K heads in N tosses 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
- Total number of ways to place X and Y at n places such that no two X are together
- Seating arrangement of N boys sitting around a round table such that two particular boys sit together
- Distance Traveled by Two Trains together in the same Direction
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Coin Change | DP-7
- Frobenius coin problem
- Coin Change | BFS Approach
- Find the player who will win the Coin game

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.