Fibonacci series in JavaScript
In this post, you will learn how to find the fibonacci series using the JavaScript programming language.
The Fibonacci series is the sequence of numbers in which the next number is the sum of the two previous numbers. The Fibonacci series was well-known hundreds of years earlier. The "Fibonacci" name came from the nickname "Bonacci".
We can easily remember the Fibonacci sequence using November 23rd as the Fibonacci Day. As November 23rd has the digits "1, 1, 2, 3", which is part of the sequence.
In the above image, the first two numbers are 0 and 1. So, according to the Fibonacci rule, the third number is 1 (sum of 0 and 1). The fourth number is 2, and so on.
0 + 1 = 1 // 0, 1, 1
1 + 1 = 2 // 0, 1, 1, 2
1 + 2 = 3 // 0, 1, 1, 2, 3
2 + 3 = 5 // 0, 1, 1, 2, 3, 5
0 ,1 , 1, 2, 3, 5, 8, 13, 21, 34....
Fibonacci series in JavaScript using for loop
In the given JavaScript program, we have used the Fibonacci series program using the for loop.
// JavaScript program
// to generate fibonacci series up to n terms
// take input from the user
var number = parseInt(prompt('Enter the number of terms: '));
var n1 = 0, n2 = 1, nextTerm;
console.log('Fibonacci Series:');
for (var i = 1; i <= number; i++) {
console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
Output of the above code:
Enter the number of terms: 7
Fibonacci Series:
0
1
1
2
3
5
8
Fibonacci series in JavaScript using while loop
In the given JavaScript program, the user is prompted to enter the number of series to print the Fibonacci series. First, we print the first and second numbers. Then we use the while loop, where we get the next number by adding the previous two numbers and simultaneously we swap the first number with the second and the second with the third.
// JavaScript program
// to generate fibonacci series
// using while loop
const number = parseInt(prompt('Enter a positive number: '));
let n1 = 0, n2 = 1, n3;
console.log('Fibonacci Series:');
console.log(n1);
console.log(n2);
n3 = n1 + n2;
while (n3 <= number) {
// print the next term
console.log(n3);
n1 = n2;
n2 = n3;
n3 = n1 + n2;
}
Output of the above code:
Enter a positive number: 6
Fibonacci Series:
0
1
1
2
3
5
Fibonacci series in JavaScript using recursion
Recursion function is a function that is called by itself. In the given JavaScript program, we call the recursion function to get the Fibonacci series. A recursion function continues until some condition is met to prevent it. That's why we use the if else statement to break the infinite recursion.
// JavaScript program
// to generate fibonacci series
// using recursion
function fibonacci(num)
{
if(num==1)
return 0;
if (num == 2)
return 1;
return fibonacci(num - 1) + fibonacci(num - 2);
}
// take input from the user
var number = parseInt(prompt('Enter the number of terms: '));
for (var i = 1; i <= number; i++) {
document.write(fibonacci(i));
}
Output of the above code:
Enter the number of terms: 8
0
1
1
2
3
5
8
13
Related Articles
Generate random numbers in JavaScriptJavascript window location
Remove duplicates from array Javascript
Scientific calculator JavaScript
How to reverse string in JavaScript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh