-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_Fibonacci.java
More file actions
35 lines (29 loc) · 862 Bytes
/
06_Fibonacci.java
File metadata and controls
35 lines (29 loc) · 862 Bytes
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
import java.util.Scanner;
public class Fibonacci {
static int fib(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
return fib(n - 1) + fib(n - 2);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int n = sc.nextInt();
System.out.println("Fibonacci Series using Iteration:");
int a = 0, b = 1;
System.out.print(a + " " + b + " ");
for (int i = 2; i < n; i++) {
int c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
System.out.println("\nFibonacci Series using Recursion:");
for (int i = 0; i < n; i++) {
System.out.print(fib(i) + " ");
}
sc.close();
}
}