VOOZH about

URL: https://www.geeksforgeeks.org/javascript/javascript-program-to-print-nth-non-fibonacci-number/

⇱ JavaScript Program to Print Nth Non Fibonacci Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JavaScript Program to Print Nth Non Fibonacci Number

Last Updated : 5 Aug, 2025

JavaScript program to print the nth non-Fibonacci number. Non-Fibonacci numbers are integers that are not part of the Fibonacci sequence.

Below are the approaches to print the Nth non-Fibonacci number:

Using a Loop

We are using a while loop to find the nth non-Fibonacci number. we iterate through Fibonacci numbers and we skip them in the count until we reach the nth non-Fibonacci number. We do this by maintaining three variables (prev1, prev2, and cur) to track the current Fibonacci numbers. We decrement N by the difference between the current Fibonacci number and the previous Fibonacci number minus 1 to skip over Fibonacci numbers. After the loop, we adjust N back to get the correct non-Fibonacci number.

Example: To demonstrate printing the nth Non Fibonacci number using a loop.


Output
15

Using Recursion

This recursive method finds the nth non-Fibonacci number by continuously calculating Fibonacci numbers. It keeps track of the count of non-Fibonacci numbers between consecutive Fibonacci numbers to determine the position of the nth non-Fibonacci number accurately. The function adjusts its search range based on this count and it stops when it finds the nth non-Fibonacci number.

Example: To demonstrate printing the nth Non Fibonacci number using recursion.


Output
10
Comment