×

Python program to find Fibonacci series

Python program to find Fibonacci series

A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence that has integers 0 and 1 at first and second place, and the rest of the sequence term will be the addition of their two previous integers.

Source Code  

The following is the source code to display the Fibonacci series:

 #Program to print Fibonacci series
 #Take user input to print number of terms
 nterms = int(input("How many terms we want:"))
 #Fisrt two term of sequence   
 t1 = 0      
 t2 = 1
 count = 0  
 #Check the validation of number of terms   
 if nterms <= 0:  
     print("Enter positive element")    
 elif nterms == 1:  
   print("Fibonacci series upto",nterms,":")   
print(t1)                                                                                                 else:  
    print("Fibonacci sequence:")        
    while count < nterms:     
print(t1)   
        nth = t1 + t2    
        #Update values      
        t1 = t2            
        t2 = nth      
        count += 1 

Output

Here is the result:

Python program to find Fibonacci series

Explanation: In this program, we have first declared the variable nterms that will store the number of terms to display the Fibonacci series. Next, we need to initialize the first term to 0 and the second term to 1. Whenever the number of terms is more than 2, a while loop is executed to find the next term of the series by adding the previous two values. Next, we will swap the variable, update it, and continue this process to the final terms. Finally, it will display the result on the screen.

Using Recursion

The following is a source code to get the Fibonacci sequence using recursion:

 #Program to print Fibonacci series using recursion                                                                   
 #Create a function to return nth terms of the sequence                                                             
 def recursionFibonacci(n):  
    if n <= 1:      
        return n        
    else:    
        return(recursionFibonacci(n-1) + recursionFibonacci(n-2))                                            
 # take input from the user    
 nterms = int(input("How many terms we want:"))                                                                 
 # check if the number of terms is valid                                                                                        
 if nterms <= 0:   
    print("Enter a positive integer")                                                                                             
 else:  
    print("Fibonacci sequence:")     
    for i in range(nterms):     
        print(recursionFibonacci(i))   

Output

Here is the result:

Python program to find Fibonacci series

Explanation: In this program, we have first declared the variable nterms that will store the number of terms to display the Fibonacci series. Next, calculate the nth term of the sequence using a recursive function recursionFibonacci(). Then, we use the loop for iteration and calculate each term recursively. Finally, it will display the result on the screen.


Related Topics

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

Python Subprocess Call Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

Python vs Java

There are a lot of programming languages out there, and every day, a new programming language is developing in some parts of the world. Each programming language is a mixture...

5 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Io stringio Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

7 minutes read.

Python Online Compiler

Compose, Run and Offer Python code internet involving OneCompiler's Python online compiler free of charge. It's one of the hearty, highlight-rich web-based compilers for python language, supporting both the adaptations, which...

3 minutes read.

Python String islower() method

Python String islower() method The string.islower() method returns a boolean value true if all cased characters in the string are lowercase and for  any other value is returns false otherwise. Syntax string.islower() Parameter NA Return This...

1 minute read.

Is Python Object Oriented Programming language

In this tutorial, we will see whether python also belongs to an object-oriented programming language like several others. We will also see the advantages of using OOPs. Further, we will...

4 minutes read.

Difference between Expression and Statement in Python

What is an expression in Python? Expression is a combination of operands and operators. Expression helps us to produce some other values. In the python programming language,  expressions produce some other value...

6 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Pltpcolor in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Weight Conversion GUI using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python logging Module

Python logging Module Introduction By logging word we understand the tracking of the events which happens when we run some software. Logging process is very important part for developing software, debugging...

12 minutes read.

Return Statement In Python

Python Return Statement The return statement is generally used for the execution ending and returns the value back to the caller. The return statement can return all types of values and...

2 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

To Do GUI Application using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.