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.
Fibonacci Series
//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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment