Sunday, June 2, 2013

Fibonacci Series

Question: We have to ask user to enter the no. of terms he/she wants, and series should be generated accordingly.

Answer: Copy the below code and paste it into "your.php" file. After done this you will just need to run "your.php" file in browser and you will get the output successfully.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Fibonancy series</title>
    </head>
    <body>
        <h1><center>Fibonancy series</center></h1>
        <?php
            $a = 0;
            $b = 1;
            $sum = 0;
        ?>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post">
            Enter number of terms:<input type="text" name="name">
            <input type="submit" name="submit" value="OK"><br>
        </form>
        <?php
            if (isset($_POST['submit'])) {
                $term = $_POST['name'];
                echo "$a,$b,";
                while ($term - 2) {
                    $sum = $a + $b;
                    $a = $b;
                    $b = $sum;
                    $term--;
                    echo $sum . ",";
                }
            }
        ?>
    </body>
</html>

No comments:

Post a Comment