Skip to content

Commit 4d6e69d

Browse files
Merge pull request TheAlgorithms#126 from dpacmen/patch-1
add FibToN
2 parents dfe5943 + 9b4ae39 commit 4d6e69d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Misc/FibToN.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Scanner;
2+
3+
public class FibToN {
4+
5+
public static void main(String[] args) {
6+
//take input
7+
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
12+
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);
17+
18+
int next = first+ second;
19+
first = second;
20+
second = next;
21+
}
22+
}
23+
24+
}

0 commit comments

Comments
 (0)