Fibonacci series Example Program In C++
On this page (6sections)
About this program
This is an example program in c common example program. Read the concept first: C++ Variables and Data Types, then study the code and output below.
Definition
In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence:
1 1 2 3 5 8 13 21 34 55 89 144 ......
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
Fibonacci series Example Program
/* Example Program For Fibonacci Series Using Loop In C++
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
// Variable Declaration
int counter, n;
long last = 1, next = 0, sum;
// Get Input Value
cout << "Enter the Number :";
cin>>n;
//Fibonacci Series Calculation
while (next < n / 2) {
cout << last << " ";
sum = next + last;
next = last;
last = sum;
}
// Wait For Output Screen
getch();
return 0;
}
Sample Output
Enter the Number :300
1
1
2
3
5
8
13
21
34
55
89
144
233
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
- C++ Variables and Data Types — Tutorial — data types and variable declaration.
- Factorial Using Loop Example Program In C++ — More in c common example program.
- Factorial Using Function Example Program In C++ — More in c common example program.
Frequently Asked Questions
What does this C++ program do?
It is a C++ example program that demonstrates Fibonacci series, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data, reading user input and user-defined functions, illustrating a common pattern in C++ programming.