-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
73 lines (55 loc) · 1.5 KB
/
Copy pathFibonacci.java
File metadata and controls
73 lines (55 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//import java.util.Scanner;
//
//public class Fibonacci {
// public static void main(String[] args) {
// Scanner in = new Scanner(System.in);
// int num=in.nextInt();
// int n1=0;
// int n2=1;
// for (int i=2;i<=num;i++){
// int temp=n2;
// n2=n2+n1;
// n1=temp;
// //System.out.println(n2);
// }
// System.out.println(n1);
// }
//}
//
//
//
//// int a = 0;
//// int b = 1;
//// int count = 2;
////
//// while (count <=n) {
//// int temp = b;
//// b = b+a;
//// a = temp;
//// count++;
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
in.close(); // Close scanner to avoid resource leak
if (num < 0) {
System.out.println("Invalid input! Enter a non-negative number.");
return;
}
int n1 = 0, n2 = 1;
if (num == 0) {
System.out.println(n1);
} else if (num == 1) {
System.out.println(n2);
} else {
for (int i = 1; i < num; i++) {
int temp = n2;
n2 = n2 + n1;
n1 = temp;
System.out.println(n2);
}
//System.out.println(n2);
}
}
}