Create a mechanism to pass in a positive integer and display all the values of the Fibonacci series up to and including the specified value

心不动则不痛 提交于 2019-12-02 09:28:46

Your error is at this lines :

for ($i; $i < $powerof; $i++) {
    $max = $max * $temp;
}

After this loop, the $max var is equal to 5^$powerof, which is not what you want. Just remove this part and it should work fine.

Also change your test from

while ($z < $max) {

to

while ($z <= $max) {

if you want to include the maximum value.

<label for="powerof">Fibonacci: </label>
<input type="text" name="powerof" value="<?php echo $_GET['powerof']; ?>"/>
<input type="submit" name='Go' value="Calculate" />
</fieldset>
</form>
<?php
$message = 'The fibonacci sequence is: <br />1<br />2<br />';
$powerof = 0;
$max = 10;
$temp = $max;

if (isset($_GET['powerof'])) {
    $powerof = $_GET['powerof'];
}

if ($powerof > 100) {
    $powerof = 100;
    $message = 'Sorry, your input was too high. I converted it to the maximum value of 100.<br />The fibonacci sequence is: <br />1<br />2<br />';

}

$x = 0;
$y = 1;
$z = 0;
$counter = 0;

while ($counter < $powerof) {
    if ($counter <= 1) {
        $z = 1;
    } else {
        $z = $x + $y;
    }
    echo($z."<br />"); 
    $x = $y;
    $y = $z;
    $counter++;
}

?>

this is the working version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!