1. Member
Join Date
Jan 2009
Posts
21
Rep Power
0

## [SOLVED] for loop?

Hello to all. I wrote this code: It prints out a list of numbers, but does anybody know how they can be multiplied?

class Testnoo

{

public static void main (String [] args)

{

int foundAt = 3;
int x = 365 - foundAt;
float q = 365;
float p;
double product;

for (p = 365; p >= x; p --)
{

product = p/q;

System.out.println(product);

}

}//end main

}//end class

2. how the numbers printed out can be multiplied? Is that what your asking?

If so you could put the 'product' value into an array, this would collect the list of numbers.

You could then create another for loop which cycles the array and multiplies the values within.

3. ... whats so hard about multiplying?
a = b * c;
System.out.println(a);
please be more specific. And another tip is give your variables meaningful names, 1 letter names that are completely irrelevant make code very hard to read.
-MK12

4. Member
Join Date
Jan 2009
Posts
21
Rep Power
0
Yes its multiplying the numbers that are printed out.

MK12. a = b*c

what would be the values for a b and c? I'm confused.

5. What do you want them to be? What's the purpose of this?
ex. double a = 5.648458; double b = Math.PI; System.out.print(a*b);
-MK12

6. It depends what you are actually wanting to multiply..

I don't understand what you are trying to do, what is the purpose of the program, why do you want to multiply the numbers, what is your disired output..

It's hard to help when you give so little information :|

7. Member
Join Date
Jan 2009
Posts
21
Rep Power
0
The numbers are probabilities. I have to multiply them together.

the multiplied numbers just give a combined probability

if you run the program above it prints out four numbers.

you know like when you have a dice and you are trying to get the probability that the second roll will be different from the first.

the first roll will be probability 6/6

the second roll will be probability 5/6 that this number is different from the first

the third roll will be probability 4/6 that this number is different from the first two

e.t.c - you have to multiply them all together and subtract from 1

8. So is your question just how to do the math? There's nothing to change it when you put in program except multiplication becomes * and division becomes /.
-MK12

9. Member
Join Date
Jan 2009
Posts
21
Rep Power
0
giving the probability that x rolls of the die will share the same number

10. Member
Join Date
Jan 2009
Posts
21
Rep Power
0
I wanted the program to multiply the numbers at it went through the loop. I just dunno how to do it .

11. Which numbers do you want it to multiply? It would be helpful If you reposted your code in Code tags to preserve format so that we can read it, and maybe show the parts you need help at in different colours.
-MK12

12. Member
Join Date
Jan 2009
Posts
21
Rep Power
0
i figured it out.

boy am i a dumbass. lol. thanks for your help.

13. This may be your solution, check over it, make sure you understand it.

Java Code:
```class Testnoo

{

public static void main (String[] args)

{

int foundAt = 3;
int x = 365 - foundAt;
float q = 365;
float p;
double currentProduct = 1.00;
double previousProduct = 1.00;
double multiProduct;

for (p = 365; p >= x; p --)
{
//calculate the individual probability
currentProduct = p/q;

//output the individual probability
System.out.println("Probability: " + currentProduct);

//calculate the overall probability
multiProduct = currentProduct * previousProduct;

//output the overall probability
System.out.println("Overall Probability: " + multiProduct);

//set the current probability to previous
previousProduct = currentProduct;

}

}

}```

14. Cool you figured it out, post the solution and check that the thread is solved please :)

( To set the thread as solved, go to, thread tools > solved )

15. Member
Join Date
Jan 2009
Posts
21
Rep Power
0
Java Code:
```class Testno

{

public static void main (String [] args)

{

int foundAt = 2;
int x = 365 - foundAt;
float q = 365;
float p;
double product = 1;

for (p = 365; p >= x; p --)
{

product = product*p/q;

}

double r = 1 - product;

System.out.println(r);

}//end main

}//end class```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•