Advertisement
Khoa98

câu 1

Feb 14th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. public class main {
  2.  
  3.     public static void main(String[] args) {
  4.         Scanner sn = new Scanner(System.in);
  5.         int n;
  6.         System.out.print("Nhập n: ");
  7.         n = sn.nextInt();
  8.         System.out.print("Dãy các số fibonacci tới n: ");
  9.         for (int i = 1; i <= n; i++) {
  10.             if (fibonacci(i)<=n) {
  11.                 System.out.print(fibonacci(i)+" ");
  12.             }
  13.         }
  14.     }
  15.  
  16.     public static int fibonacci(int n) {
  17.         if (n == 2 || n == 1) {
  18.             return 1;
  19.         } else {
  20.             return fibonacci(n - 1) + fibonacci(n - 2);
  21.         }
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement