
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Probability of Getting a Sum on Throwing 2 Dices N Times in C++
We are given with the sum and the number of times the pair of dice is thrown as an input and the task is to determine the probability of getting the given sum on throwing a pair of dice N times.
Probability is the chances of getting the desired output from the set of data available. The range of probability lie between 0 and 1 where an integer 0 shows the chances of impossibility and 1 indicates certainty.
Example
Input-: sum = 12, N = 1 Output-: Probability = 1/36 Explanation-: if a pair of dice is thrown once then the combinations will be (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6). Out of these combinations we can get sum 12 at pair (6, 6) only therefore probability would be 1/36 Input-: sum = 4 and N = 6 Output-: probability is : 1/2985984
Approach used in the below program is as follows −
- Input the value of sum and N which indicates the number of times the dice is thrown
- For calculating the probability of occurring sum on throwing 2 dices N times apply this formula : (favorable/total) ^ N
- Now calculate the probability of occurring that sum on thrown of 2 dice 1 times which will be let’s sa 1
- for calculating the Probability of occurring that sum on thrown of 2 dice N times it will be −
- Probability2 = (Probability 1) ^ N. i.e Probability1 raise to power N
Algorithm
Start Step 1-> declare function to calculate the probability int probability(int sum, int times) Declare and set float res = 0.0 and total = 36.0 Declare and set long int probab = 0 Loop For i = 1 and i <= 6 and i++ Loop For j = 1 and j <= 6 and j++ IF ((i + j) = sum) Set res++ End End End Declare and set int gcd1 = __gcd((int)res, (int)total) Declare and set res = res / (float)gcd1 Set total = total / (float)gcd1 Set probab = pow(total, times) return probab Step 2-> In main() Declare and set int sum = 4 and times = 6 Call probability(sum, times) Stop
Example
#include <bits/stdc++.h> using namespace std; // function that calculates the Probability of getting a sum on throwing 2 Dices N times int probability(int sum, int times) { float res = 0.0, total = 36.0; long int probab = 0; for (int i = 1; i <= 6; i++) { for (int j = 1; j <= 6; j++) { if ((i + j) == sum) res++; } } int gcd1 = __gcd((int)res, (int)total); res = res / (float)gcd1; total = total / (float)gcd1; probab = pow(total, times); return probab; } int main() { int sum = 4, times = 6; cout<<"probability is : "; cout << "1" << "/" << probability(sum, times); return 0; }
Output
probability is : 1/2985984
Advertisements