Simple factorial function
June 8th, 2009 by Seich
Well, today my sister was working doing her homework and, she kind of got stressed with it, so she called me and, I ended up making it for her… I learned what a factorial is and, made this nice little function that finds the factorial of any number you pass to it. It was supposed to be written in labview, but I don’t really know what that is… so I used php xD It worked like a charm in labview according to my sister. so you can also use it in labview I guess :O
1 2 3 4 5 6 7 8 9 | <?php function factorial($input){ $output = 1; for($x = $input; $x >= 1; $x--){ $output *= $x; } return($output); } ?> |
I made an example of this function’s usage using JavaScript, check it out here
function factorial($n){
return $n == 1 ? 1:$n * factorial($n – 1);
}
My sister was required to use a for loop ^^ plus idk what labview’s capabilities are so, I kept it as basic as possible. I like your function nevertheless, it is short and simple.
how to get input for the same function.
what do you mean?
can you explain it with the help of getting input with a form by post method..
Here can u please give me a page “process.php” with a code getting input.
something like this would get the information from a form and then have it echo the factorial.
process.php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$f = $_POST['factorial'];
echo(factorial($f));
function factorial($input){
$output = 1;
for($x = $input; $x >= 1; $x--){
$output *= $x;
}
return($output);
}
?>
I hope that fulfills your request.