In this tutorial you will learn about the C++ Programs to Generate Fibonacci Series and its application with practical example.
C++ Programs to Generate Fibonacci Series
In this tutorial, we will learn to create a C++ program that will Reverse Array Element Using Function using C++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ programming.
- For loop in C++ Programming.
- Functions in C++ Programming.
What is Fibonacci Series?
The Fibonacci series is a series that is strongly related to Binet’s formula (golden ratio). in the Fibonacci series, the series starts from 0, 1 and then the third value is generated by adding those numbers,
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
STEP 1: START STEP 2: INITIALIZE n STEP 3: Sending the message to inter the value of n from user STEP 4: Grabbing the n number element from the user STEP 5: Using that number in for loop and calculations STEP 6: Generating the fibonacci series STEP 7: Printing the series. STEP 8: Return 0. STEP 10: END. |
Programs to Generate Fibonacci Series:-
In this program, we take the number of elements for the series from the user. Then we will generate the Fibonacci series from the below program code.
Program:-
Program to Generate Fibonacci Series
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> using namespace std; int main() { //declaring the variables required for the programs. int n, t1 = 0, t2 = 1, nterm = 0; //Taking input number from the user for upto n terms. cout << "Enter the number of terms: "; cin >> n; //Printing the fibonacci Series upto the n terms. cout << "Fibonacci Series: "; for (int i = 1; i <= n; ++i) { // Prints the first two terms. if(i == 1) { cout << t1 << ", "; continue; } if(i == 2) { cout << t2 << ", "; continue; } nterm = t1 + t2; t1 = t2; t2 = nterm; //Printing the terms fo fibonacci Series cout << nterm << ", "; } return 0; } |
Output:-
In the above program, we have first initialized the required variable
- t1 = it will hold the integer value.
- t2 = it will hold the integer value.
- nterm = it will hold the integer value.
- n = it will hold the integer value.
- i = it will hold the integer value to control the array.
Generating the fibonacci series.
Program Code for Printing the series.