Home C++ Fibonacci Series in C++ and Python

Fibonacci Series in C++ and Python

C++ and Python are the most popular languages of this time. They both are used in Artificial Intelligence, Machine Learning, Scientific Programming, Developing Software, etc. C++ is a swift language, and it is also similar to C and close to the hardware, so we can write very efficient and fast programs using it.

It is also a Compiled type language, which compiles the source code into binary before running. Python is not as fast as C++, but it is way too simpler and powerful than C++, making it popular among developers. It is an interpreted language, i.e., it uses an interpreter instead of a compiler.

This tutorial will use both python and C++ to write programs that print the Fibonacci Series. We will use two techniques to print the Fibonacci series, one using loops and the other by using recursion. It is recommended to run the programs on your own computer and see how they work. To do this, you need to first install C++ and python on your computer. You can also use an IDE like Visual Studio Code for writing your code faster.

Introduction

Fibonacci series is a series of numbers in which the next number is the sum of the previous two numbers, and it starts from 0 and 1. For Eg: See the below sequence of numbers.

0 1 1 2 3 5 8 13 21 34 55 …

The Fibonnaci Series is defined by a reccurence relation which is shown below-

F(n) = F(n-1) + F(n-2)

F(0) = 0 and F(1) = 1

Now, let us see how can we use programming languages to print Fibonacci Series to a given number of terms.

Displaying Fibonacci Series without Recursion

Fibonacci series can be displayed by using the normal looping. To achieve this, we need to create three variables, first, second, and temp, and then initialize them with 0, 1, and 0. We need to use for loop to iterate for several terms and store the sum of the first and second in the temp variable. Then store the second variable value to the first variable and the temp variable value to the second variable and print the second variable for each iteration. This will print the Fibonacci series for the number of terms.

Using C++

Now let us look at the C++ code for printing the Fibonacci series using the looping method discussed above. See the below code block for an illustration.

// including the iostream header file
#include <iostream>
using namespace std;
// The main function of the code
int main()
{
    int first = 0, second = 1, temp = 0, n, i;
    cout << "[+] Enter the Number of terms : ";
    cin >> n;
    cout << first << endl
         << second << endl;
    // calculating and displaying the fibonacci series
    for (i = 0; i < n - 1; i++)
    {
        temp = first + second;
        cout << temp << endl;
        first = second;
        second = temp;
    }
    return 0;
}

In the above code, we first include the iostream header file in our code that is necessary for input-output streaming. We declare the line for using namespace std. After that, we started our main() function of the code. In the main function, we first declare four variables of integer type viz. first, second, temp, i and request the user to enter the number of terms for which the program will print the Fibonacci series.

We print the first two numbers of the series 0 and 1. After that, we loop for (n-2) times starting from 0 and store the sum of the value of the first and second variable in a temp variable and the value of the second variable first and the value of temp in the second and also print the temp variable value.

Displaying Fibonacci series using C++ programming and for loop without recursion
Displaying Fibonacci series using C++ programming and for loop without recursion

Using Python

We have seen that how we can print the Fibonacci series using the C++ programming language. Now let us see how we can do the same operation using the Python programming language. See the below code for illustration.

# Program for displaying fibonacci series
n = int(input("Enter The number of terms : "))
first = 0
second = 1
print(first)
print(second)
# Calculating and printing the fibonacci series
for i in range(0, (n-1)):
    temp = first + second
    first = second
    second = temp
    print(second)

In the above code, first, we took the number of terms as an input from the user and stored it in a variable n. We initialize the variable first with 0 and second with 1 and print them. We use the python for loop to iterate for the range 0 to n-1 and assign the sum of first and second in the temp variable. Then assign the variable second into first and the temp into the second. Next, we print the value of the second variable.

Displaying Fibonacci series using python programming and for loop without recursion
Displaying Fibonacci series using python programming and for loop without recursion

Displaying Fibonacci Series Using Recursion

In the above topics, we have looked at displaying the Fibonacci series by just using for loops. Now we see how we can do the same operation using recursion. Recursion is the process of calling a function in its own definition. Recursion is a good method for writing large code in a simple way. But sometimes, it also complexes the code that can be written in a simple way. Let us see how we can display the Fibonacci series using Recursion.

Using C++

Here we will see how we can use recursion to print the Fibonacci series using the C++ programming language. See the below code for illustration.

// including the iostream header file
#include <iostream>
using namespace std;
// Function to calculate the fibonacci series
int fib(int n)
{
    if (n <= 1)
        return n;
    // performing recursion
    return fib(n - 1) + fib(n - 2);
}
int main()
{
    int n, i;
    cout << "Enter the number of terms : ";
    cin >> n;
    // calculating fibonacci series for n terms
    for (i = 0; i <= n; i++)
    {
        cout << fib(i) << endl;
    }
    return 0;
}

We first include the iostream header file in the above code, which provides us basic functionality for the input and output streaming. Then we create a function with the name fib(), which accepts one argument, i.e., the term for which we want to calculate the Fibonacci number. This function will use recursion for calculating the Fibonacci number for a given term.

We created the main() function for our C++ code. In the main function, we prompt the user to enter the number of terms we need to calculate the Fibonacci series. We use the for loop to iterate from 0 to n and call the function inside the loop and pass the iterative variable in the function’s argument and print the return of the function.

Displaying the fibonacci series using C++ programing and recursion
Displaying the fibonacci series using C++ programing and recursion

Using Python

We have seen how can we display the fibonacci series in C++ using recursion. Now let us see how can we perform the same operation using Python programming langauge.

# Program for displaying fibonacci series using recursion
n = int(input("Enter The number of terms : "))
first = 0
second = 1
# Function for calculating fibonacci number
def fib(n):
    if n <= 1:
        return n
    # performing recursion
    return fib(n - 1) + fib(n - 2)
# displaying the fibonacci series for n terms
for i in range(0, n+1):
    print(fib(i))

In the above code, first, we take the number of terms as input from the user. Then we initialize two variables first and second with zero and one respectively. We created a function fib(n) which takes the term as its argument for which we need to calculate the Fibonacci number. The function calculates the Fibonacci number for that term using recursion. We use python’s for loop to iterate from 0 to n+1 and for each iteration, we call the fib function with the argument as the iterative variable.

Displaying the fibonacci series using python programiing and recursion
Displaying the fibonacci series using python programiing and recursion

Conclusion

In this tutorial, we have seen how to display the Fibonacci series by using two methods. We have used the for loop and also the recursion technique to print the Fibonacci series. We have written the code in two popular languages, viz. C++ and Python so it can be understood by both C++ and Python programmers. You may also want to see our tutorial on printing spiral matrix using C++ and how to find the sum of the elements of a list in python.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More