Given two coins which have probability of getting heads *p%* and *q%* respectively, the task is to determine the probability of getting two consecutive heads after choosing random coins among the given coins.

**Examples:**

Input:p = 33, q = 66

Output:0.550000000000000

Input:p = 33, q = 66

Output:0.550000000000000

**Approach:**

Since both the coins are not identical so Bayes’s theorem will be used to get the desired probability.

As the coins are to be chosen randomly then any of them can be chosen so *p* and *q* will be included in the calculation. After applying Bayes’s theorem, the required answer will be *(p * p + q * q) / (p + q)* because if the first coin is chosen then the probability of getting both heads back to back is *p * p* and same for the second coin.

It’s an application of Bayes’ theorem.

P(B | A) = P(A |^| B) / P(A) = (1/2 * p * p + 1/2 * q * q) / (1/2 * p + 1/2 * q) = (p * p + q * q) / (p + q) where;

P(B) = probability to get heads on the second throw,

P(A) = probability to get heads on the first throw and

P(A |^| B) = probability to get heads on both the throws.

So, P(B | A) is the probability to get heads on the second throw if we are given that we got heads

on the first one.

Here A, B denotes 1st and 2nd coin.

Below is the implementation of above approach:

## C++

`// C++ program to get the probability ` `// of getting two consecutive heads ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the probability ` `// of getting two consecutive heads ` `double` `getProbability(` `double` `p, ` `double` `q) ` `{ ` ` ` `p /= 100; ` ` ` `q /= 100; ` ` ` ` ` `// Formula derived from Bayes's theorem ` ` ` `double` `probability = (p * p + q * q) / (p + q); ` ` ` `return` `probability; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `double` `p, q; ` ` ` ` ` `// given the probability of getting ` ` ` `// a head for both the coins ` ` ` `p = 80; ` ` ` `q = 40; ` ` ` ` ` `cout << fixed ` ` ` `<< setprecision(15) ` ` ` `<< getProbability(p, q) ` ` ` `<< endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to get the probability ` `// of getting two consecutive heads ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the probability ` `// of getting two consecutive heads ` `static` `double` `getProbability(` `double` `p, ` `double` `q) ` `{ ` ` ` `p /= ` `100` `; ` ` ` `q /= ` `100` `; ` ` ` ` ` `// Formula derived from Bayes's theorem ` ` ` `double` `probability = (p * p + q * q) / (p + q); ` ` ` `return` `probability; ` `} ` ` ` `// Driver code ` ` ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `double` `p, q; ` ` ` ` ` `// given the probability of getting ` ` ` `// a head for both the coins ` ` ` `p = ` `80` `; ` ` ` `q = ` `40` `; ` ` ` ` ` `System.out.println( getProbability(p, q)); ` ` ` `} ` `} ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to get the probability ` `# of getting two consecutive heads ` ` ` `# Function to return the probability ` `# of getting two consecutive heads ` `def` `getProbability(p, q): ` ` ` ` ` `p ` `/` `=` `100` ` ` `q ` `/` `=` `100` ` ` ` ` `# Formula derived from Bayes's theorem ` ` ` `probability ` `=` `(p ` `*` `p ` `+` `q ` `*` `q) ` `/` `(p ` `+` `q) ` ` ` `return` `probability ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# given the probability of getting ` ` ` `# a head for both the coins ` ` ` `p ` `=` `80` ` ` `q ` `=` `40` ` ` ` ` `print` `(getProbability(p, q)) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to get the probability ` `// of getting two consecutive heads ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the probability ` `// of getting two consecutive heads ` `static` `double` `getProbability(` `double` `p, ` `double` `q) ` `{ ` ` ` `p /= 100; ` ` ` `q /= 100; ` ` ` ` ` `// Formula derived from Bayes's theorem ` ` ` `double` `probability = (p * p + q * q) / (p + q); ` ` ` `return` `probability; ` `} ` ` ` `// Driver code ` ` ` ` ` ` ` `public` `static` `void` `Main () { ` ` ` `double` `p, q; ` ` ` ` ` `// given the probability of getting ` ` ` `// a head for both the coins ` ` ` `p = 80; ` ` ` `q = 40; ` ` ` ` ` `Console.WriteLine( getProbability(p, q)); ` ` ` `} ` `} ` `// This code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to get the probability ` `// of getting two consecutive heads ` ` ` `// Function to return the probability ` `// of getting two consecutive heads ` `function` `getProbability(` `$p` `, ` `$q` `) ` `{ ` ` ` `$p` `/= 100; ` ` ` `$q` `/= 100; ` ` ` ` ` `// Formula derived from ` ` ` `// Bayes's theorem ` ` ` `$probability` `= (` `$p` `* ` `$p` `+ ` `$q` `* ` `$q` `) / ` ` ` `(` `$p` `+ ` `$q` `); ` ` ` `return` `$probability` `; ` `} ` ` ` `// Driver code ` ` ` `// given the probability of getting ` `// a head for both the coins ` `$p` `= 80; ` `$q` `= 40; ` ` ` `echo` `getProbability(` `$p` `, ` `$q` `); ` ` ` `// This code is contributed ` `// by Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0.666666666666667

## Recommended Posts:

- Probability of not getting two consecutive heads together in N tosses of coin
- Probability of getting K heads in N coin tosses
- 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
- Count of total Heads and Tails after N flips in a coin
- Expected number of coin flips to get two heads in a row?
- Probability of choosing a random pair with maximum sum in an array
- Make a fair coin from a biased coin
- Expected Number of Trials to get N Consecutive Heads
- Probability of getting a perfect square when a random number is chosen in a given range
- Maximize sum by choosing elements from different section of a matrix
- Probability of distributing M items among X bags such that first bag contains N items
- Minimum size binary string required such that probability of deleting two 1's at random is 1/X
- Random number generator in arbitrary probability distribution fashion
- Probability of a random pair being the maximum weighted pair
- Select a Random Node from a tree with equal probability
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Discrete Random Variables - Probability | Class 12 Maths
- Count of elements which are second smallest among three consecutive elements
- Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)

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.