Home / javascript / How to Create and get nth Fibonacci series number?
Fibonacci series
Fibonacci series Images

How to Create and get nth Fibonacci series number?

What is the Fibonacci series?

Fibonacci series is the series of numbers that are calculated by adding the values of the previous two numbers in the sequence. The first two numbers of the sequence are zero and one. It is also known as the golden ratio. We can represent the Fibonacci sequence as shown below.

Formula of Fibonacci series number

F(n) = F(n-1) + F(n-2), where F(0) = 0 and F(1) = 1 as the first two numbers of the sequence.

The integer sequence of the Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233……nth

It is one of the most asked interview questions.

Example – 1

function fibonacciSeries(n){
  var fibonac = [0, 1];
  
  if (n <= 2) return 1;

  for (var i = 2; i <=n; i++ ){
    fibonac[i] = fibonac[i-1]+fibonac[i-2];
    console.log(fibonac[i]); 
// Print here Fibonacci series: 1 2 3 5 8 13 21 34 55
  }

 return fibonac[n];
} 

console.log(fibonacciSeries(10));  // Output is 55

About admin

Check Also

What is hoisting in javascript with example?

Hoisting in JavaScript is a behavior where variable and function declarations are moved to the …

Leave a Reply

Your email address will not be published. Required fields are marked *