How to usewhile Loop in PHP
PHP loop can generate Fibonacci numbers until reaching a number greater than or equal to the given number. If the given number is found during the process, it is a Fibonacci number.
Example:
PHP
<?php function isFibonacci( $n ) { $a = 0; $b = 1; while ( $b < $n ) { $temp = $a + $b ; $a = $b ; $b = $temp ; } return ( $b == $n ); } // Driver code $n = 13; if (isFibonacci( $n )) { echo "Fibonacci Number" ; } else { echo "Not a Fibonacci Number" ; } ?> |
Output
Fibonacci Number
Check if a Given Number is Fibonacci Number in PHP
Given a number N, the task is to check whether the given number is a Fibonacci number or not.
Examples:
Input: 21
Output: Fibonacci Number
Input: 35
Output: Not a Fibonacci Number