Fibonacci Series in C++, Fibonacci Series program in C++, Fibonacci Series example program
Write a C++ program to print Fibonacci series of n terms where n is input by the programmer.

Program Code:
#include <iostream>
using namespace std;
int main()
{

int number;
int number1 = 0;
int number2 = 1; 
int next_number = 0;
cout << "Enter a positive number: ";
cin >> number;
cout<<endl;
// displays the first two numbers of series which is always 0 and 1
cout << "Fibonacci Series: " ;
cout<< number1 << ", " << number2 << ", ";
// now third number of the series is the addition of its previous two number
next_number = number1 + number2;

while(next_number <= number)
{
cout << next_number << ", ";
number1 = number2;
number2 = next_number;
next_number = number1 + number2;
}
return 0;
}


The program output is tested on www.jdoodle.com
Output:
Enter a positive number: 30
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21


Thanks
Mukesh Rajput

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: