text: Nearest Fibonacci Number to N | Function to find the Fibonacci number which is nearest to N ; Base Case ; Initialize the first & second terms of the Fibonacci series ; Store the third term ; Iterate until the third term is less than or equal to num ; Update the first ; Update the second ; Update the third ; Store the Fibonacci number having smaller difference with N ; Print the result ; Driver Code
code: function nearestFibonacci ( num ) { if ( num == 0 ) { document . write ( 0 ) ; return ; } let first = 0 , second = 1 ; let third = first + second ; while ( third <= num ) { first = second ; second = third ; third = first + second ; } let ans = ( Math . abs ( third - num ) >= Math . abs ( second - num ) ) ? second : third ; document . write ( ans ) ; } let N = 17 ; nearestFibonacci ( N ) ;