- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
- Related Articles
- Find the probability of getting the multiple of 6 on throwing a dice
- Maximum number of dots after throwing a dice N times in C++
- How to create sample space of throwing two dices in R?
- Probability of getting at least K heads in N tosses of Coins in C++
- If a coin is tossed 2 times, what is the probability of (1) Getting head at least once? (2) Getting exactly one head?
- Probability of rain on N+1th day in C++
- A coin is tossed 100 times and head is obtained 65 times .Find the probability of getting 1 head in 2 toss.
- Evaluate: $frac{a^{2 n+1} times a^{(2 n+1)(2 n-1)}}{a^{n(4 n-1)}times(a^{2})^{2 n+3}}$.
- Two dice are thrown simultaneously. The probability of getting a sum of $9$.
- Two different dice are tossed together. Find the probability :$( i)$ of getting a doublet $( ii)$ of getting a sum 10, of the numbers on the two dice.
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- A coin is tossed 100 times and the head is obtained 59 times. On tossing a coin at random, the probability of getting a head is?
- Sum of the series 1.2.3 + 2.3.+ … + n(n+1)(n+2) in C
- Sample space of throwing a die and find the probability of:1. Getting A no. Less than 42. Getting A composite no3. Getting A nos. Which are multiple of 34. Getting even nos. 5. Getting odd nos. 6. Getting a no. Greater than six7. Getting a number less than one8. A prime no
- Find sum of Series with n-th term as n^2 - (n-1)^2 in C++

Advertisements