Home  • Programming • JavaScript

Display Fibonacci Series in JavaScript

In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence: 0,1,1,2,3,5,8,13,21,34,55 ... or (often, in modern usage): 1,1,2,3,5,8,13,21,34,55 ... Following code prints Fibonacci series: number between 0-10
<script>
var n=10, first =0, second=1, next;

for(i=0;i<=n;i++){
      if(i <= 1){
         next=i;
      }else{
         next=first+second;
         first=second;
         second=next;
      }
     document.write(next+",");
}
</script>
output:0,1,1,2,3,5,8,13,21,34,55

Comments 1


Thanks sir...
Copyright © 2024. Powered by Intellect Software Ltd