# [SOLVED] for loop?

Printable View

• 02-16-2009, 08:04 PM
hawaiifiver
[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
• 02-16-2009, 08:15 PM
sirwiggles
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.
• 02-16-2009, 08:28 PM
MK12
... 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
• 02-16-2009, 08:34 PM
hawaiifiver
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.
• 02-16-2009, 08:39 PM
MK12
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
• 02-16-2009, 08:40 PM
sirwiggles
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 :|
• 02-16-2009, 08:48 PM
hawaiifiver
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
• 02-16-2009, 08:50 PM
MK12
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
• 02-16-2009, 08:51 PM
hawaiifiver
giving the probability that x rolls of the die will share the same number
• 02-16-2009, 08:53 PM
hawaiifiver
I wanted the program to multiply the numbers at it went through the loop. I just dunno how to do it .
• 02-16-2009, 08:54 PM
MK12
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
• 02-16-2009, 09:24 PM
hawaiifiver
i figured it out.

boy am i a dumbass. lol. thanks for your help.
• 02-16-2009, 09:28 PM
sirwiggles
This may be your solution, check over it, make sure you understand it.

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;         }     } }```
• 02-16-2009, 09:30 PM
sirwiggles
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 )
• 02-17-2009, 07:12 PM
hawaiifiver
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```