| 3 | public class fibo{ |
| 4 | |
| 5 | public static void main(String[] args) { |
| 6 | // write your code here |
| 7 | |
| 8 | int a = 0; |
| 9 | int b=1; |
| 10 | |
| 11 | Scanner sc = new Scanner(System.in); |
| 12 | |
| 13 | int n= sc.nextInt(); |
| 14 | sc.close(); |
| 15 | |
| 16 | for(int i=0; i<n; i++){ |
| 17 | |
| 18 | System.out.print(a+" "); |
| 19 | |
| 20 | int c = a+b; |
| 21 | |
| 22 | a=b; |
| 23 | b=c; |
| 24 | } |
| 25 | // fibbnacci using dynamic programming |
| 26 | |
| 27 | public static int fibdp(int n) { // bottom up approach |
| 28 | int storage[]=new int[n+1]; |
| 29 | storage[0]=0; |
| 30 | storage[1]=1; |
| 31 | for(int i=2;i<=n;i++) { |
| 32 | storage[i]= storage[i-1]+storage[i-2]; |
| 33 | } |
| 34 | return storage[n]; |
| 35 | } |
| 36 | |
| 37 | public static void main(String[] args) { |
| 38 | int n=44; |
| 39 | System.out.println(fibM(n)); |
| 40 | System.out.println(fibdp(n)); |
| 41 | |
| 42 | System.out.println(fib(n)); |
| 43 | |
| 44 | |
| 45 | } |
| 46 | } |