Tuesday, 26 December 2017

HTML Progtamming

Develop and demonstrate a XHTML file that includes Javascript script for the following problems:
a) Input : A number n obtained using prompt
Output : The first n Fibonacci numbers
b) Input : A number n obtained using prompt
Output : A table of numbers from 1 to n and their squares using alert
Objective :-
To get acquainted with javascript and how to embed javascript in html code.
Proceedure :-
Question 2a :-
1. Declare the script tag as text/javascript in the beginning of the <body> of html program
2. Get the number of Fibonacci elements to be generated from the user using prompt()
3. Validate input given and alert the user for invalid input using alert()
4. Generate the Fibonacci numbers using the standard algorithm and print it to std out using
document.write()
<!--
------------------------------------------------------------XHTML CODE--------------------------------------------------
-->
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Fibonacci Numbers </title>
</head>
<body>
<h1>Calculating the fibonacci numbers</h1>
<script type="text/javascript">
var n,a=0,b=1,i,c
n=prompt("Enter a number ","")
if(n<=0) alert("Invalid number")
else
{
if(n==1) document.write(a)
else document.write(a+"<br />"+b)
for(i=2;i<n;i++)
{
c=a+b
a=b
b=c
document.write("<br />"+c)
}
}
</script>
</body>
</html>
                                  <!-- End of File -->

No comments:

Post a Comment