Skip to content

Commit a1ac1e7

Browse files
committed
add FibonacciNumber.js
1 parent 72dcb82 commit a1ac1e7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function fibonacci(p) {
2+
if(p === 1 || p === 2) {
3+
return p-1; // base case
4+
} else {
5+
6+
let a = 0;
7+
let b = 1;
8+
// 0 1 1 2 3 5 8 ...
9+
let fib = 0;
10+
for(var i = 2 ; i < p ; i = i + 1) {
11+
fib = a + b;
12+
a = b;
13+
b = fib;
14+
}
15+
16+
return fib;
17+
}
18+
}
19+
20+
21+
const readline = require('readline').createInterface({
22+
input: process.stdin,
23+
output: process.stdout
24+
});
25+
26+
readline.question("Enter value of n ", n => {
27+
console.log(fibonacci(n));
28+
readline.close()
29+
});
30+
31+

0 commit comments

Comments
 (0)