top of page
Writer's pictureNew Era Education

Fibonacci Series Programs in C++

Fibonacci Series Programs in C++

Let’s  see the way to learn the Fibonacci series programs using various methods in C++  like recursion, loop, array, without recursion, etc:

  1. Without the Recursion Method

Code:

#include<iostream> using namespace std; int main() { int first_num = 0, second_num = 1, third_num, i, num; cout << “Enter random number to print fibonacci series:”; cin >> num; cout << “Fibonacci Series for a given number:” <<endl; cout << first_num  <<endl; cout << second_num << endl; for(i = 2; i < num; ++i) //loop will starts from 2 because we have printed 0 and 1 before { third_num = first_num + second_num; cout << third_num << endl; first_num = second_num; second_num = third_num; } return 0; }

  1. By Using Recursion Method

Code:

#include<iostream> using namespace std; void FibonacciSeries(int num) { static int first_num = 0, second_num = 1, third_num; if(num > 0) { third_num = first_num + second_num; first_num = second_num; second_num = third_num; cout << third_num << endl; FibonacciSeries(num – 1); } } int main() { int num; cout << “Enter random number to print fibonacci series:”; cin >> num; cout << “Fibonacci Series for a given number: \n” << endl; cout << “0” << endl; cout << “1” << endl; FibonacciSeries (num – 2); //number-2 is used because we have already print 2 numbers return 0; }

  1. Using a For loop

Code:

#include<iostream> using namespace std; int main() { int first_num= 0, second_num = 1, third_num, i, num; cout << “Enter random number to print fibonacci series:”; cin >> num; cout << “Fibonacci Series for a given number:” <<endl; cout << first_num <<endl; cout <<  second_num <<endl; for(i = 0; i < num; i++) //loop will starts from 2 because we’ve got printed 0 and 1 before { if(i <= 1) third_num = i; else { third_num = first_num + second_num; cout << third_num <<endl; first_num = second_num; second_num = third_num; } } return 0; }

  1. By Using While Loop

Code:

#include<iostream> using namespace std; int main() { int first_num = 0, second_num = 1, third_num = 0, i = 3, num; cout << “Enter random number to print fibonacci series:” ; cin >> num; cout << “Fibonacci Series for a given number:” <<endl; cout <<  first_num <<endl; cout << second_num <<endl; while(i <= num) { third_num = first_num + second_num; cout << third_num <<endl; first_num = second_num; second_num = third_num; i = i + 1; } return 0; }

  1. Using an array

Code:

#include<iostream> using namespace std; int main() { int fibonacci[25], i, num; cout << “Enter random number to print fibonacci series:”; cin >> num; fibonacci[0] = 0; fibonacci[1] = 1; for (i = 2; i < num; i++) { fibonacci[i] = fibonacci[i – 1] + fibonacci[i – 2]; } cout <<“Fibonacci Series for a given number:” << endl; for (i = 0; i < num; i++) { cout << fibonacci[i] <<endl; } return 0; }

Fibonacci series program in C++ using specified number

Code:

#include<iostream> using namespace std; int main() { int first_num = 0, second_num = 1, third_num, i; cout << “Fibonacci Series for a given number:”; cout << first_num <<endl; cout << second_num <<endl; for(i = 2; i < 10; ++i) //loop will starts from 2 because we’ve got printed 0 and 1 before { third_num = first_num + second_num; cout << third_num <<endl; first_num = second_num; second_num = third_num; } return 0; }

Related Link

5 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page