JavaScript Program to Print the Fibonacci Sequence

To understand this example, you should have the knowledge of the following JavaScript programming topics:


A fibonacci sequence is written as:

0, 1, 1, 2, 3, 5, 8, 13, 21, ...

The Fibonacci sequence is the integer sequence where the first two terms are 0 and 1. After that, the next term is defined as the sum of the previous two terms.


Example 1: Fibonacci Series Up to n Terms

Output

Enter the number of terms: 4
Fibonacci Series:
0
1
1
2

In the above program, the user is prompted to enter the numbers of terms that they want in the Fibonacci series.

The for loop iterates up to the number entered by the user.

0 is printed at first. Then, in each iteration, the value of the second term is stored in variable n1 and the sum of two previous terms is stored in variable n2.


Example 2: Fibonacci Sequence Up to a Certain Number

Output

Enter a positive number: 5
Fibonacci Series:
0
1
1
2
3
5

In the above example, the user is prompted to enter a number up to which they want to print the Fibonacci series.

The first two terms 0 and 1 are displayed beforehand. Then, a while loop is used to iterate over the terms to find the Fibonacci series up to the number entered by the user.