BUT2/DEV/DEV3.2/TP02_Recursivite/Q3Fibonacci.java

36 lines
763 B
Java
Raw Normal View History

2023-10-12 16:39:49 +02:00
import java.lang.*;
public class Q3Fibonacci{
int tour;
public Q3Fibonacci(){
tour = -1;
}
public int fibonacci(int n) {
this.tour ++;
this.espace(n);
if (n < 0){
return -1;
}
if (n == 0){
this.espace(0);
this.tour --;
return 0;
}
if (n == 1){
this.espace(1);
this.tour --;
return 1;
}
int resultat = fibonacci(n-2) + fibonacci(n-1);
this.espace(resultat);
this.tour --;
return resultat;
}
private void espace(int txt){
for (int i=0; i<this.tour; i++){
System.out.print(" ");
}
System.out.println(txt);
}
}