updated
added comments and changed variable names for better understanding
This commit is contained in:
parent
0afa2da3ce
commit
9b4ae39291
@ -3,18 +3,21 @@ import java.util.Scanner;
|
||||
public class FibToN {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//take input
|
||||
Scanner scn = new Scanner(System.in);
|
||||
int N = scn.nextInt();
|
||||
// print fibonacci sequence less than N
|
||||
int first = 0, second = 1;
|
||||
//first fibo and second fibonacci are 0 and 1 respectively
|
||||
|
||||
int n = scn.nextInt();
|
||||
while(first <= N){
|
||||
//print first fibo 0 then add second fibo into it while updating second as well
|
||||
|
||||
int fn = 0, sn = 1;
|
||||
System.out.println(first);
|
||||
|
||||
while(fn <= n){
|
||||
System.out.println(fn);
|
||||
|
||||
int next = fn + sn;
|
||||
fn = sn;
|
||||
sn = next;
|
||||
int next = first+ second;
|
||||
first = second;
|
||||
second = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user