<form method="get" action="Index.php">
<fieldset>
<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 = 5;
$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 />';
}
$i = 1;
for ($i; $i < $powerof; $i++) {
$max = $max * $temp;
}
$x = 1;
$y = 2;
$z = $x + $y;
echo($message);
while ($z < $max) {
$z = $x + $y;
echo($z . "<br />");
$x = $y;
$y = $z;
}
?>
this is my code and the issue is that if when i enter 1 into the text box rather than it just showing the 1st number within the sequence it shows the first 4 so for 2 it displays 1 2 3 5 8 13 21 34 rather than just 1 2, any ides guys???
this is the working version
Your error is at this lines :
After this loop, the
$max
var is equal to5^$powerof
, which is not what you want. Just remove this part and it should work fine.Also change your test from
to
if you want to include the maximum value.