-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
19 lines (18 loc) · 790 Bytes
/
Fibonacci.java
File metadata and controls
19 lines (18 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(); //taking input from user
int a = 0; //initializing a with 0
int b = 1; //initializing b with 1
int count = 1; //initializing count with 1 because 1st digit i.e 0 will already be printed before entering while loop
System.out.print(a + " "); //this will print 0 before we enter the while loop
while(count<=n-1){
int counter = b;
b = b + a;
a = counter;
count++;
System.out.print(a + " ");
}
}
}