Given four integers **a**, **b**, **c** and **d**. Player **A** & **B** try to score a penalty. Probability of A shooting the target is **a / b** while probability of B shooting the target is **c / d**. The player who scores the penalty first wins. The task is to find the probability of A winning the match.

**Examples:**

Input:a = 1, b = 3, c = 1, d = 3

Output:0.6

Input:a = 1, b = 2, c = 10, d = 11

Output:0.52381

**Approach:** If we consider variables **K = a / b** as the probability of A shooting the target and **R = (1 – (a / b)) * (1 – (c / d))** as the probability that A as well as B both missing the target.

Therefore, the solution forms a Geometric progression **K * R ^{0} + K * R^{1} + K * R^{2} + …..** whose sum is

**(K / 1 – R)**. After putting the values of

**K**and

**R**we get the formula as

**K * (1 / (1 – (1 – r) * (1 – k)))**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the probability of A winning ` `double` `getProbability(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` ` ` `// p and q store the values ` ` ` `// of fractions a / b and c / d ` ` ` `double` `p = (` `double` `)a / (` `double` `)b; ` ` ` `double` `q = (` `double` `)c / (` `double` `)d; ` ` ` ` ` `// To store the winning probability of A ` ` ` `double` `ans = p * (1 / (1 - (1 - q) * (1 - p))); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 1, b = 2, c = 10, d = 11; ` ` ` `cout << getProbability(a, b, c, d); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the probability ` `// of A winning ` `static` `double` `getProbability(` `int` `a, ` `int` `b, ` ` ` `int` `c, ` `int` `d) ` `{ ` ` ` ` ` `// p and q store the values ` ` ` `// of fractions a / b and c / d ` ` ` `double` `p = (` `double` `) a / (` `double` `) b; ` ` ` `double` `q = (` `double` `) c / (` `double` `) d; ` ` ` ` ` `// To store the winning probability of A ` ` ` `double` `ans = p * (` `1` `/ (` `1` `- (` `1` `- q) * ` ` ` `(` `1` `- p))); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a = ` `1` `, b = ` `2` `, c = ` `10` `, d = ` `11` `; ` ` ` `System.out.printf(` `"%.5f"` `, ` ` ` `getProbability(a, b, c, d)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the probability ` `# of A winning ` `def` `getProbability(a, b, c, d) : ` ` ` ` ` `# p and q store the values ` ` ` `# of fractions a / b and c / d ` ` ` `p ` `=` `a ` `/` `b; ` ` ` `q ` `=` `c ` `/` `d; ` ` ` ` ` `# To store the winning probability of A ` ` ` `ans ` `=` `p ` `*` `(` `1` `/` `(` `1` `-` `(` `1` `-` `q) ` `*` `(` `1` `-` `p))); ` ` ` ` ` `return` `round` `(ans,` `5` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `1` `; b ` `=` `2` `; c ` `=` `10` `; d ` `=` `11` `; ` ` ` `print` `(getProbability(a, b, c, d)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the probability ` `// of A winning ` `public` `static` `double` `getProbability(` `int` `a, ` `int` `b, ` ` ` `int` `c, ` `int` `d) ` `{ ` ` ` ` ` `// p and q store the values ` ` ` `// of fractions a / b and c / d ` ` ` `double` `p = (` `double` `) a / (` `double` `) b; ` ` ` `double` `q = (` `double` `) c / (` `double` `) d; ` ` ` ` ` `// To store the winning probability of A ` ` ` `double` `ans = p * (1 / (1 - (1 - q) * ` ` ` `(1 - p))); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `a = 1, b = 2, c = 10, d = 11; ` ` ` `Console.Write(` `"{0:F5}"` `, ` ` ` `getProbability(a, b, c, d)); ` `} ` `} ` ` ` `// This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the probability ` `// of A winning ` `function` `getProbability(` `$a` `, ` `$b` `, ` `$c` `, ` `$d` `) ` `{ ` ` ` ` ` `// p and q store the values ` ` ` `// of fractions a / b and c / d ` ` ` `$p` `= ` `$a` `/ ` `$b` `; ` ` ` `$q` `= ` `$c` `/ ` `$d` `; ` ` ` ` ` `// To store the winning probability of A ` ` ` `$ans` `= ` `$p` `* (1 / (1 - (1 - ` `$q` `) * (1 - ` `$p` `))); ` ` ` `return` `round` `(` `$ans` `,6); ` `} ` ` ` `// Driver code ` `$a` `= 1; ` `$b` `= 2; ` `$c` `= 10; ` `$d` `= 11; ` `echo` `getProbability(` `$a` `, ` `$b` `, ` `$c` `, ` `$d` `); ` ` ` `// This code is contributed by chandan_jnu ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0.52381

## Recommended Posts:

- Find probability that a player wins when probabilities of hitting the target are given
- Write a function that generates one of 3 numbers according to given probabilities
- Find the minimum possible health of the winning player
- Find the ratio of number of elements in two Arrays from their individual and combined average
- Find amount to be added to achieve target ratio in a given mixture
- Finding the probability of a state at a given time in a Markov chain | Set 2
- Probability of getting a perfect square when a random number is chosen in a given range
- Find minimum moves to reach target on an infinite line
- Minimum moves to reach target on a infinite line | Set 2
- Count of all possible ways to reach a target by a Knight
- Generate integer from 1 to 7 with equal probability
- Random number generator in arbitrary probability distribution fashion
- Probability of Knight to remain in the chessboard
- Probability of getting at least K heads in N tosses of Coins
- Aptitude | Probability | Question 1
- Aptitude | Probability | Question 2
- Aptitude | Probability | Question 3
- Aptitude | Probability | Question 4
- Aptitude | Probability | Question 5
- Aptitude | Probability | Question 6

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.