We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0afa2da commit 9b4ae39Copy full SHA for 9b4ae39
Misc/FibToN.java
@@ -3,18 +3,21 @@
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
- int n = scn.nextInt();
-
- int fn = 0, sn = 1;
- while(fn <= n){
13
- System.out.println(fn);
+ 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
- int next = fn + sn;
- fn = sn;
- sn = next;
18
+ int next = first+ second;
19
+ first = second;
20
+ second = next;
21
}
22
23
0 commit comments