Program in Python to print Fibonacci sequence
 

Program in Python to print Fibonacci sequence

 

A Fibonacci sequence is consists of integer numbers whose first and second term is 0 and 1 respectively you can determine the coming terms by adding the previous two numbers of the sequence.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 . . . . .

You can say that the nth term of the sequence is the sum of (n-1)th and (n-2) th term.

In this article, we will use recursion to write our program in Python to print the Fibonacci sequence.

What is recursion?

The recursion occurs when a thing is defined in terms of itself. In programming, a function which calls itself either directly or indirectly is called a recursive function. Using recursion some problems can be solved very easily.

Here we will use recursion to write our Python program that will print Fibonacci series.

Source code of Python program to print Fibonacci sequence

You can execute the following program to print Fibonacci sequence.

def fibonacci_seq(n):
    if(n == 0):
      return 0
    elif(n == 1):
      return1
    else:
     return (fibonacci_seq(n - 2) + fibonacci_seq(n - 1))
n = int(input("Enter number of terms you want to print:  "))
print("Fibonacci Series:", end = ' ')
for n in range(0, n):
    print(fibonacci_seq(n), end = ' ')

Now if you execute this program it will print the Fibonacci sequence up to the number of terms you entered.

There are different ways in which you can write this program. In the above program, we used recursion to print Fibonacci sequence.

Now if you have a query then write use in the comments below.

Previous Python List vs Array
Next Python program to check whether a number or string is palindrome or not
 

About author

You might also like

Programming 0 Comments

Escape sequences in Python

An escape sequence or escape character allows you to insert a special character in a string. For example, you can insert a new line in a string using \n character.

Programming 0 Comments

Introduction To The Course On Python Programming

Python is a high-level, interpreted, general-purpose programming language. It emphasizes more on code readability so its syntax is simple and easy to use. People from different areas use it for

Programming 0 Comments

Taking Input From Users In Python

Computer programs often need to interact with users to get some kind of data from them. Later this data is processed and output is displayed on the console. For example,

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply