×

C++ Fibonacci Series

What is a Fibonacci series?

A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of the previous two elements. It simply means that the upcoming term will be easily figured out by applying the previous terms' logic. To be clearer with how the Fibonacci series work, let us consider an example.

For example:  1  2  3  5  8  13 …………N

Here, we can see that the previous elements starting from 1 then 2. We have assigned 1 and 2 as 1st and 2nd terms initially. Then we added 1 & 2, then 2 & 3 and so on till nth term.

Let us now discover different approaches of generating Fibonacci series till N terms.

  1. The novice approach

Consider the code given below:

Fibonacci Series in C++

Output:

Fibonacci Series in C++

Explanation: In the above code, we are taking input from the sequence. We took number 10 so ten terms of the Fibonacci numbers are generated on the console. The logic behind is to simply loop over the terms till 10 and then consequently adding them using next term variable. It is quite easy to understand since the approach is traditional.

  • The recursive approach

Consider the code given below:

Fibonacci Series in C++

Output:

Fibonacci Series in C++

Explanation:

In this program, we expect to get the nth element at the position in the palindrome series. We are taking input from the user and have constructed a function named “fibonacci” where the parameter is the number that is being taken from the user and is compared using condition such that if the number is <=1. The Fibonacci series will be the addition of (n-1) and (n-2) terms. This is because we are gradually finding the next elements respective of their positions in the container.

Logic and formulations of Fibonacci series

The Fibonacci numbers are usually of the following sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...N

Mathematically, a Fibonacci series is formulated using the following recurrence relation

Fibonacci Series in C++

Here,

      Fn = Fibonacci sequence where n is the no. of terms.

      F(n-1) = one element before the last occurrence .

      F(n-2) = second-last element the last occurrence.

We always assign first and second element in the series as 0 and 1 and in the novice approach we will start our loop from 2 since the first and second position are already assigned as 0 and 1.

Let us now look at some other approaches associated with Fibonacci series.

  • Dynamic Programming Method

Consider the code given below:

Fibonacci Series in C++

Output:

Fibonacci Series in C++

Explanation:

Here, sequence is generated using the summation of (n-1) and (n-2).

We have previously used recursive approach to solve the Fibonacci series but it is simpler in dynamic approach. It can store numbers in a table and we can easily generate the next sequential terms.


Related Topics

Print Table Using For-loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

3 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

Maps in C++

Maps: Maps in C++ are the containers associated with key and mapped values. By keys and mapped values, we mean that the maps are used to store elements formed by the...

4 minutes read.

Inheritance in C++ vs Java

Just like we inherit traits from our parents, object-oriented programming has a concept called inheritance. In terms of object-oriented programming, a class's traits and behaviours, or its data and methods,...

4 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

Message Passing in C++

The act of sending and receiving information by an object is referred to as communication, and all communication between objects that takes place via message is known as message passing....

1 minute read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

C++ Switch

In C++, an expression or variable can be tested against a range of constant values using a switch statement, which is a control flow statement. It offers a productive method...

4 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

Armstrong number using for loop in C++

What is For Loop? A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax that can be...

4 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

C++ Iterators

What are iterators ? Iterators are among the four foundations of the C++ Standard Template Library, also known as the STL. The memory address of the STL container classes is pointed...

15 minutes read.

Single level Inheritance

Inheritance is a fundamental element of C++’s Object-Oriented Programming (OOP). It allows a class (called the derived class) to inherit characteristics and attributes from another class (called the base class)....

5 minutes read.

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.