Skip to content

Commit 9b4ae39

Browse files
authored
updated
added comments and changed variable names for better understanding
1 parent 0afa2da commit 9b4ae39

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

Misc/FibToN.java

+12-9
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
public class FibToN {
44

55
public static void main(String[] args) {
6+
//take input
67
Scanner scn = new Scanner(System.in);
8+
int N = scn.nextInt();
9+
// print fibonacci sequence less than N
10+
int first = 0, second = 1;
11+
//first fibo and second fibonacci are 0 and 1 respectively
712

8-
int n = scn.nextInt();
9-
10-
int fn = 0, sn = 1;
11-
12-
while(fn <= n){
13-
System.out.println(fn);
13+
while(first <= N){
14+
//print first fibo 0 then add second fibo into it while updating second as well
15+
16+
System.out.println(first);
1417

15-
int next = fn + sn;
16-
fn = sn;
17-
sn = next;
18+
int next = first+ second;
19+
first = second;
20+
second = next;
1821
}
1922
}
2023

0 commit comments

Comments
 (0)