Fibonacci Series Program in JavaScript

Put Your Plan Into Action

Fibonacci Series Program in JavaScript

Simple JavaScript Program

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html>
	<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(isNaN(n)){
				alert("ENTER ONLY INTEGER NUMBERS");
				location.reload();
			}
			else{
				if(n<=0){
					alert("Invalid Number");
					location.reload();
				}
				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>

 

Leave a Reply

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