-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP13_FibonacciSeries.java
More file actions
45 lines (39 loc) · 1.41 KB
/
Copy pathP13_FibonacciSeries.java
File metadata and controls
45 lines (39 loc) · 1.41 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
package programs;
/**
* ============================================================
* PROGRAM 13: Fibonacci Series Generator
* ============================================================
* Problem: WAP to generate the Fibonacci series up to N terms:
* - Iterative approach (O(n) time, O(1) space)
* - Recursive approach
* Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
* ============================================================
*/
public class P13_FibonacciSeries {
public static void printFibonacciIterative(int terms) {
if (terms <= 0) return;
System.out.printf("Fibonacci (%d terms): ", terms);
long a = 0, b = 1;
for (int i = 1; i <= terms; i++) {
System.out.print(a + " ");
long next = a + b;
a = b;
b = next;
}
System.out.println();
}
public static long fibonacciRecursive(int n) {
if (n <= 0) return 0;
if (n == 1) return 1;
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}
public static void main(String[] args) {
System.out.println("=== ITERATIVE FIBONACCI ===");
printFibonacciIterative(10);
printFibonacciIterative(15);
System.out.println("\n=== RECURSIVE Nth FIBONACCI TERM ===");
for (int i = 0; i <= 8; i++) {
System.out.printf(" fib(%d) = %d%n", i, fibonacciRecursive(i));
}
}
}