An XHTML program to display first n Fibonacci numbers using Javascript

By | August 1, 2012

Input: A number n obtained using prompt

Output: The first n Fibonacci numbers

 

<? xml version=”1.0″ encoding=”UTF-8″?>

<!–

            Document : fib

            Created on : Mar 28, 2009, 10:50:21 PM

            Author :Admin

–>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<title> fib.html </title>

</head>

<body style=”background-color:yellow”>

<h3 style=”text-align:center; color:red”> 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 :<br/>”);

document.write(“”,f1,”<br/>”);

document.write(“”,f2,”<br/>”);

var i,f3;

for(i=2; i<limit; i++)

{

f3=f1+f2;

document.write(“”,f3,”<br/>”);

f1=f2;

f2=f3;

}

</script>

</body>

</html>

 

The output of this program is as follows

   

Please Share: Tweet about this on TwitterShare on FacebookShare on Google+Share on RedditPin on PinterestShare on LinkedInDigg thisShare on StumbleUponShare on TumblrBuffer this pageShare on VKEmail this to someone

One thought on “An XHTML program to display first n Fibonacci numbers using Javascript

  1. Manjunath

    This code is not working on my Crome and IE browser… Showing Errors as like below
    This page contains the following errors:

    error on line 8 at column 17: AttValue: ” or ‘ expected
    Below is a rendering of the page up to the first error.

    Program to generate the Fibonacci Series
    and the program is-

    fib.html

    Program to generate the Fibonacci Series

    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, “”);
    document.write(“The fibonacci series :”);
    document.write(“”,f1,””);
    document.write(“”,f2,””);
    var i,f3;
    for(i=2; i<limit; i++)
    {
    f3=f1+f2;
    document.write(“”,f3,””);
    f1=f2;
    f2=f3;
    }

    Reply

Leave a Reply

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