Hướng dẫn fibonacci series program in python - chương trình chuỗi fibonacci trong python

Trong chương trình này, bạn sẽ học cách in trình tự Fibonacci bằng cách sử dụng vòng lặp.

Để hiểu ví dụ này, bạn nên có kiến ​​thức về các chủ đề lập trình Python sau:

  • Python nếu ... tuyên bố khác
  • Python trong khi vòng lặp

Trình tự Fibonacci là chuỗi số nguyên là 0, 1, 1, 2, 3, 5, 8 ....

Hai thuật ngữ đầu tiên là 0 và 1. Tất cả các thuật ngữ khác có được bằng cách thêm hai thuật ngữ trước đó. Điều này có nghĩa là để nói thuật ngữ thứ n là tổng của (n-1) và thuật ngữ (n-2).

Mã nguồn

# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid
if nterms <= 0:
   print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
# generate fibonacci sequence
else:
   print("Fibonacci sequence:")
   while count < nterms:
       print(n1)
       nth = n1 + n2
       # update values
       n1 = n2
       n2 = nth
       count += 1

Đầu ra

How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8

Ở đây, chúng tôi lưu trữ số lượng các điều khoản trong NTERMS. Chúng tôi khởi tạo thuật ngữ đầu tiên thành 0 và thuật ngữ thứ hai thành 1.

Nếu số thuật ngữ là nhiều hơn 2, chúng tôi sử dụng vòng lặp while để tìm thuật ngữ tiếp theo trong chuỗi bằng cách thêm hai thuật ngữ trước đó. Sau đó, chúng tôi trao đổi các biến (cập nhật nó) và tiếp tục với quy trình.

Bạn cũng có thể giải quyết vấn đề này bằng chương trình đệ quy: Python để in trình tự Fibonacci bằng cách sử dụng đệ quy.

How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
62
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
63
# Python program to display the Fibonacci sequence

def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))
4
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
14
   F0 = 0 and F1 = 1.
5
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
50

How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
62
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
14
# Python program to display the Fibonacci sequence

def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))
4
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
50


How many terms? 7 Fibonacci sequence: 0 1 1 2 3 5 8 62How many terms? 7 Fibonacci sequence: 0 1 1 2 3 5 8 18# Python program to display the Fibonacci sequence def recur_fibo(n): if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) nterms = 10 # check if the number of terms is valid if nterms <= 0: print("Plese enter a positive integer") else: print("Fibonacci sequence:") for i in range(nterms): print(recur_fibo(i)) 4 How many terms? 7 Fibonacci sequence: 0 1 1 2 3 5 8 75

# Python program to display the Fibonacci sequence

def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))

Vui lòng tham khảo hoàn thành bài viết về chương trình cho các số Fibonacci để biết thêm chi tiết! & NBSP;

Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34

Trình tự Fibonacci là chuỗi số nguyên là 0, 1, 1, 2, 3, 5, 8 .... To test the program, change the value of nterms.

Hai thuật ngữ đầu tiên là 0 và 1. Tất cả các thuật ngữ khác có được bằng cách thêm hai thuật ngữ trước đó. Có nghĩa là điều này để nói thuật ngữ thứ n là tổng của (n-1) và thuật ngữ thứ cấp.

Mã nguồn

Đầu ra

How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
1
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
14
# Python program to display the Fibonacci sequence

def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))
4
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
4

How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
1
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
18
# Python program to display the Fibonacci sequence

def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))
4
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
6

How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
1
# Python program to display the Fibonacci sequence

def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))
2
# Python program to display the Fibonacci sequence

def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))
3
# Python program to display the Fibonacci sequence

def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))
4
# Python program to display the Fibonacci sequence

def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = 10

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))
4
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
4
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
5

  • How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4
  • How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    3
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5
  • How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    14
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    18
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    3
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
    In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation 

        Fn = Fn-1 + Fn-2

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    3
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5

       F0 = 0 and F1 = 1.

    Phương pháp 1 (sử dụng đệ quy): & nbsp; & nbsp;  

    Python3

    def

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    0

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    2
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    3
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    7
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    8
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    9
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    0

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    3
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    3
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    7

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
        Fn = Fn-1 + Fn-2
    7
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
       F0 = 0 and F1 = 1.
    1
       F0 = 0 and F1 = 1.
    2
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6
       F0 = 0 and F1 = 1.
    4
       F0 = 0 and F1 = 1.
    5
       F0 = 0 and F1 = 1.
    1
       F0 = 0 and F1 = 1.
    22
        Fn = Fn-1 + Fn-2
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    0

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    7while1while2while3

    Phương pháp 2 (sử dụng lập trình động): & nbsp; & nbsp;  

    Python3

    while4

    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4 while6
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4while8
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6recur_fibo()0

    def recur_fibo()2

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    2
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    3
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    7
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    8
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    9
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    0

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    3
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
        Fn = Fn-1 + Fn-2
    7def3

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    3
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    7

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    7
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    08while2while3

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    3
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    7
      

    How many terms? 7 Fibonacci sequence: 0 1 1 2 3 5 8 6# Python program to display the Fibonacci sequence def recur_fibo(n): if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) nterms = 10 # check if the number of terms is valid if nterms <= 0: print("Plese enter a positive integer") else: print("Fibonacci sequence:") for i in range(nterms): print(recur_fibo(i)) 9 Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34 6

    def recur_fibo()2

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    3for6for7

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9 def0

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    2
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    3
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    7
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    8
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    9
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    0

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    3
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    2
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    3
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    7

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    1
        Fn = Fn-1 + Fn-2
    7
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    5

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
       F0 = 0 and F1 = 1.
    1
       F0 = 0 and F1 = 1.
    2
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6
       F0 = 0 and F1 = 1.
    4
       F0 = 0 and F1 = 1.
    5
       F0 = 0 and F1 = 1.
    1
       F0 = 0 and F1 = 1.
    22
        Fn = Fn-1 + Fn-2
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    0

    Phương pháp 2 (sử dụng lập trình động): & nbsp; & nbsp;

    while4

    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    4 while6
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    4while8
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6recur_fibo()0

    def recur_fibo()2

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    7
    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    08while2while3

    How many terms? 7
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    
    6
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    9
       F0 = 0 and F1 = 1.
    1
       F0 = 0 and F1 = 1.
    2
    Fibonacci sequence:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    
    6
       F0 = 0 and F1 = 1.
    4
       F0 = 0 and F1 = 1.
    5
       F0 = 0 and F1 = 1.
    1
       F0 = 0 and F1 = 1.
    22
        Fn = Fn-1 + Fn-2
    1
    # Python program to display the Fibonacci sequence
    
    def recur_fibo(n):
       if n <= 1:
           return n
       else:
           return(recur_fibo(n-1) + recur_fibo(n-2))
    
    nterms = 10
    
    # check if the number of terms is valid
    if nterms <= 0:
       print("Plese enter a positive integer")
    else:
       print("Fibonacci sequence:")
       for i in range(nterms):
           print(recur_fibo(i))
    
    0