can anybody help me out with code?
i want to write java program to calculate the probability that sum of two numbers is divisible by 4 or 6 when two dice thrown
Printable View
can anybody help me out with code?
i want to write java program to calculate the probability that sum of two numbers is divisible by 4 or 6 when two dice thrown
Okay? And your question is?
My question is how to write java code to solve this
With a text editor?
IOW, we are not simply going to give you finished code.
Give it, at least, a try. If/When you run into problems, post your code and a specific question, and we will be glad to help, but we are not a "homework service".
dude i am really stucked with it...
i dnt know how to approach this problem using java
i thought this community is to help beginners...
So write a method that determines all possibilites (and keeps a count of them), select all those that are divisible by 4 or 6 (and keeps a count of them, use %), then divide the two.
That is, at least, possibility, which is what you want, I believe, actual probablity also uses how many rolls were made, and how many of those already fit the criteria, but even with those, the principle remains the same.
Where is the problem?
Edit: It is to help beginners, not to do their (home)work for them. You will have received descriptions of the algorithm to use, and will have received instructions on, at the least the basics of, Class construction, so write something. If you have a problem we will be gald to help, but we are not going to write for you. That would not help you, regardless of how much you think it might.
yep...the sample space for this problem is 36 as there r 2 dice
so i have to use 2 for loops?
If you mean one loop per die with an if statement and two counters inside the second, that is one definate possibility.
see i worked it out
class Probability
{
public void calculate()
{
double sampleSpace=36;
double count=0;
double probability=0;
for(int i =1;i<=6;i++)
{
for(int j=1;j<=6;j++)
{
if((i+j)%4==0 || (i+j)%6==0)
{
count++;
}
}
}
probability=count/sampleSpace;
System.out.println("Probability is:"+probability);
}
public static void main(String args[])
{
new Probability().calculate();
}
}