×

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

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.

std::min in C++

std::min in C++ std::min is specified in the program code, used to calculate the lowest amount that has been transferred. When there's more of someone who returns first of them. It's used...

2 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

3 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

7 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

Diamond Pattern using While-loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

5 minutes read.

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

Print Table Using Do while 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...

4 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

C++ Reading file

In file handling, read() function is used to read data from the file into the program. The read() uses ifstream library to read data from a file. Syntax file-stream-class   file-stream-object;   file-stream-object.read((char *)&var , sizeof (var)); <h3">Example ofstream  outfile;         outfile . read((char*)&emp,sizeof(emp)); C++ File Handling read() Function Example Reading the content of existing...

2 minutes read.

Difference between exit() and _Exit() in C++

Before understanding the difference between the exit() and _Exit(), one must know about exit() and _Exit() functions. The exit() function in C/C++ The exit() method in the C language kills the calling...

3 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Destructor

Let’s discover the C++ destructor, virtual and pure virtual destructors, and when destructors are invoked and their rules in this lesson. Destructors : A member function that destroys an object is known as...

5 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

Different Ways to Compare Strings in C++

This section will go over the many methods for comparing strings in the C++ programming language. The string comparison checks if the first string is equal to another string. HELLO...

6 minutes read.

Virtual Functions and Runtime Polymorphism in C++

In this tutorial, we will explore more on virtual functions and runtime polymorphism in the most useful language C++. A virtual function is a member function with the keyword virtual used...

6 minutes read.