<Martian Wabbit Productions>

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

  • Archive

  • Tags

    COmposing factorial Feed Feedburner font function Hello loop Music Noctorious PHP Script Tutorial type Updates
  • Downloads

  • Random Ads

  • 6 Responses to “Simple factorial function”

    1. Tim Cooper says:

      function factorial($n){
      return $n == 1 ? 1:$n * factorial($n – 1);
      }

    2. Seich says:

      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.

    3. mutsil says:

      how to get input for the same function.

    4. Seich says:

      what do you mean?

    5. Mutsil says:

      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.

    6. Seich says:

      something like this would get the information from a form and then have it echo the factorial.
      process.php

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      <?php

      $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.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>