Find probability that a player wins when probabilities of hitting the target are given | Java implementation of the approach ; Function to return the probability of the winner ; Driver Code ; Will print 9 digits after the decimal point
import java . util . * ; import java . text . DecimalFormat ; class solution { static double find_probability ( double p , double q , double r , double s ) { double t = ( 1 - p / q ) * ( 1 - r / s ) ; double ans = ( p / q ) / ( 1 - t ) ; return ans ; } public static void main ( String args [ ] ) { double p = 1 , q = 2 , r = 1 , s = 2 ; DecimalFormat dec = new DecimalFormat ( " # 0.000000000" ) ; System . out . println ( dec . format ( find_probability ( p , q , r , s ) ) ) ; } }