Skip to content

Instantly share code, notes, and snippets.

@neilkerman
Created August 8, 2021 14:45
Show Gist options
  • Save neilkerman/597e35af0e8ba919c4fe84f257a3227c to your computer and use it in GitHub Desktop.
Save neilkerman/597e35af0e8ba919c4fe84f257a3227c to your computer and use it in GitHub Desktop.

Revisions

  1. neilkerman created this gist Aug 8, 2021.
    28 changes: 28 additions & 0 deletions fibonacci.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    //Javascript program to print the fibonacci series. Coded by Samay Bhattacharyya.

    //Importing the readline library to read a line from console
    const readline = require("readline");
    const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    });

    //Function to print Fibonacci Series
    var fibonacci = (n) => {
    let a = 0, b = 1;
    while (n-- > 0) {
    process.stdout.write(`${a}, `);
    b = a + b;
    a = b - a;
    }
    }

    //Using readline library to take input and run the function
    rl.question("Enter a number: ", function (num) {
    console.log(`Fibonacci Series:\n`);
    if(isNaN(num)){console.log("Kindly enter a number");rl.close();}
    fibonacci(num);
    rl.close();
    });

    //Use node fibonacci.js to run the program