Jump to content

[SOLVED] calculate product of parent rows in while loop


Zaynt

Recommended Posts

I need replace the $n in cell 2 of the table with the the product of all the parent rows

 

e.g row 5 will output the value 120 (1*2*3*4*5)

 

anyone have any idea on how to solve this problem?

 

<table border="1">
<?php
$n = 0;

while($n<25){
$n++;
echo"<tr>";
echo"<td>Row number $n</td>";
echo" <td>$n</td>";
echo"</tr>";
}
?>
</table>

If it's just a simple factorial:

function factorial($factLoop=1) {
$factorial = 1;
while ($factLoop > 1) {
	$factorial *= $factLoop--;
}
return $factorial;
}

for ($i = 1; $i < 12; $i++) {
echo factorial($i).'<br />';
}

ok...that was a little bit more complicated than I imagined. I got this assignment at school and we have been working with php for 2 days now, and have just learned how to use for,while and do while -loops. We haven't even started with functions yet, I have just played with some on my own.

 

I will try to wrap my mind around this code of yours, because it seems to work just fine. Now I just have to figure out how it works.

 

Thank you very much for the help so far(I may need some more later on)!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.