How to Generate Fibonacci Numbers In Javascript

This program explains how to generate Fibonacci numbers in Javascript. The javascript is embedded in HTML code. You can refer to the input and output images attached.

Input: A number
Output: Fibonacci numbers series for the entered number


<?xml version="1.0" encoding="UTF-8"?>
<title> fibonacci.html </title>
<body style="background-color:green"> <!-- it is advised to not use inline styling -->
<h3 style="text-align:center; color:white"> Program to generate the Fibonacci Series </h3>
<script type="text/javascript">

var limit = prompt("Enter the limit 'n' to generate the fibonacci series:", " ");
var f1=0;
var f2=1;

document.write("The limit entered to generate the fibonacci series is: ",limit, "<br/>");
document.write("The fibonacci series : ");
document.write("",f1," ");
document.write("",f2," ");
var i,f3;
for(i=2; i<limit; i++)
	 document.write("",f3," ");


Other Programs:


code to generate Fibonacci numbers using Javascript


Output of Fibonacci numbers in Javascript
The output shows Fibonacci numbers series

How to execute this script?

  1. Copy the code to a text file and save it with a .html extension.
  2. Open this file in a web browser.
  3. You will be asked to enter a number and as a result, the corresponding Fibonacci series is displayed for that number.
  4. To check for another number, refresh the web page and enter a new number.

Please let me know if you encounter any problems.

3 thoughts on “How to Generate Fibonacci Numbers In Javascript”

Did it help? Would you like to express?

%d bloggers like this: