Fibonacci Series Program in C++

Fibonacci Series Program in C++

4 mins read270 Views Comment
clickHere
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Aug 31, 2023 10:30 IST

The Fibonacci series is an intriguing mathematical idea that is popular for practicing in Python, especially for those just starting out.

2023_07_Feature-Image-Templates-75.jpg

What is a Fibonacci Series? 

A Fibonacci series or a Fibonacci sequence is given as – 

2023_02_image-58.jpg

It is a unique sequence of positive of numbers in which each number is the sum of the two preceding numbers. It starts with 0, followed by 1, and the sequence goes on as shown above. In mathematical terms, the nth term of the Fibonacci sequence is given by the formula: 

You can also explore: Understanding Inline Function in C++

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

Where, F(0) = 0 and F(1) = 1. 

The Fibonacci sequence is named after the Italian mathematician Leonardo Fibonacci, who first introduced it to the Western world in his book “Liber Abaci” written in 1202. The sequence has been widely used in various areas of mathematics, science, art, and nature, and it appears in many real-life situations such as the growth of populations, spirals in sunflowers, pine cones, and shells, etc. 

You can also explore: Understanding Multiset in C++

Fibonacci Series in Python
Fibonacci Series in Python
Fibonacci Series is a sequence of numbers where each number is the sum of the two previous numbers. This article will discuss the Fibonacci Series, How to Create a Fibonacci...read more
Fibonacci Program in C
Fibonacci Program in C
The Fibonacci Series is a sequence of numbers where each number is a sum of the previous two numbers. This article will explore how to implement the Fibonacci Series in...read more
Fibonacci Series in Java [Case-Study Based]
Fibonacci Series in Java [Case-Study Based]
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In Java, the Fibonacci...read more

Methods to Print Fibonacci Program up to Nth Term in C++ 

Using while loop 

In this method, we’ll use the while loop to print the Fibonacci series up to the input n as the range: 

 
#include <iostream>
int main() {
int n, first = 0, second = 1, next;
std::cout << "Enter the number of terms: ";
std::cin >> n;
std::cout << "Fibonacci sequence: " << first << " " << second << " ";
int i = 2;
while (i < n) {
next = first + second;
first = second;
second = next;
std::cout << next << " ";
i++;
}
return 0;
}
Copy code

Output : 

2023_02_image-56.jpg

What have we done here?  

In this code, we start by reading the value of n from the user, which is the number of terms we want to print in the sequence. Then, we initialize the variables first and second to 0 and 1, respectively, and print their values as the first two terms of the sequence. 

Next, we use a while loop to calculate and print the next terms in the sequence. The loop continues until i (the number of terms printed so far) is less than n. Inside the loop, we use the formula next = first + second to calculate the next term, update the values of first and second, and print the value of next. The loop then continues until all n terms have been printed. 

You can also explore: Hierarchical inheritance in C++ with real-life analogy

Using for loop 

In this method, we’ll use the for loop to print the Fibonacci series up to the input n as the range: 

 
#include <iostream>
int main() {
int n, first = 0, second = 1, next;
std::cout << "Enter the number of terms: ";
std::cin >> n;
std::cout << "Fibonacci sequence: " << first << " " << second << " ";
for (int i = 2; i < n; i++) {
next = first + second;
first = second;
second = next;
std::cout << next << " ";
}
return 0;
}
Copy code

Output : 

2023_02_image-59.jpg

What have we done here?  

This code is similar to the previous example using a while loop. However, instead of using a while loop, we use a for loop to accomplish the same task. The for loop continues until i (the number of terms printed so far) is less than n. Inside the loop, we use the formula next = first + second to calculate the next term, update the values of first and second, and print the value of next. The loop then continues until all n terms have been printed. 

Using recursion 

Recursion is a programming technique in which a function calls itself as a subroutine. In C++, recursion is a powerful programming tool that can be used to solve complex problems by breaking them down into smaller, simpler sub-problems. 

Recursive functions work by dividing the problem into smaller, identical sub-problems until a base case is reached, at which point the solution is constructed from the solutions of the smaller sub-problems. 

You can also explore: Ceil function in C++

Here’s an example of a recursive function in C++ to calculate the nth Fibonacci number: 

 
#include <iostream>
void printFibonacci(int first, int second, int n) {
if (n > 0) {
std::cout << first << " ";
printFibonacci(second, first + second, n - 1);
}
}
int main() {
int n, first = 0, second = 1;
std::cout << "Enter the number of terms: ";
std::cin >> n;
std::cout << "Fibonacci sequence: ";
printFibonacci(first, second, n);
return 0;
}
Copy code

Output : 

2023_02_image-60.jpg

What have we done here?  

In this code, we have a recursive function printFibonacci that takes in three arguments: the first two terms of the Fibonacci sequence, and the number of terms to print. The function uses a base case: if n is less than or equal to 0, the function simply returns.

In all other cases, the function prints the first term and then calls itself with updated values for the first two terms and decremented value for n, thus printing the next term in the sequence. This creates a chain of function calls that continues until the base case is reached, at which point the function stops calling itself and returns, effectively printing the entire Fibonacci sequence. 

You can also explore: New and delete operators in C++

Endnotes 

Fibonacci series is a fascinating mathematical concept that is widely used in various fields such as computer science, biology, and finance. In fact, the Fibonacci sequence, also known as the Golden Ratio, can be seen in the shapes of spiral galaxies, snail shells, and even flower petals.  

Hope this article was helpful for you to understand how to print a Fibonacci series in C++. Explore our C++ articles to find out more about the language and consolidate your knowledge of the fundamentals.   

FAQs

What is Fibonacci Series?

Fibonacci numbers are the sequence of numbers defined by the linear equation, F(n) = F(n-1) + F(n-2), for n = 3, 4, ... and F(0) = 0, F(1) = F(2) = 1.

What are the first 10 Fibonacci Numbers in the Fibonacci Series?

The first 10 Fibonacci numbers are, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

What is the use of Fibonacci Series?

Fibonacci Series is used in Finance (Stock Market Analysis), Computer Science (Efficient Search), Music ( Tuning Musical Instruments), Art (Creating Pleasing Visual Design).

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio