Skip to content

Commit bec51cc

Browse files
updated fibonacci method
1 parent 84164df commit bec51cc

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

src/main/java/com/thealgorithms/recursion/FibonacciSeries.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,14 @@ public final class FibonacciSeries {
1111
private FibonacciSeries() {
1212
throw new UnsupportedOperationException("Utility class");
1313
}
14-
public static int fibonacci(int n) {
15-
if (n <= 1) {
16-
return n;
17-
} else {
18-
return fibonacci(n - 1) + fibonacci(n - 2);
14+
public static void fibonacci(int n,int i , int a , int b) {
15+
if(i==n){
16+
return ;
1917
}
18+
int c = a+b ;
19+
System.out.println(" " + c + " ");
20+
fibonacci(n, i+1, b, c);
2021
}
2122

2223

23-
// n is the number upto which fibonacci sequence is required
24-
// for example n = 4 , output = 0 1 1 2
25-
public static void fibo(int n){
26-
int a = 0 ; int b = 1 ; int c =0;
27-
System.out.print(a+" "+b);
28-
for (int i = 2 ; i<n ; i++){
29-
c = a+b;
30-
System.out.print(" "+ c);
31-
a = b ;
32-
b = c ;
33-
}
34-
35-
}
36-
3724
}

0 commit comments

Comments
 (0)