Fibonacci Series or Sequence
2nd number = 1
3rd number = 0+1= 1
4th number = 1+1= 2
5th number = 1+2= 3
And so on.
The Fibonacci Sequence can be written as 0,1,1,2,3,5,8,13........
C++ program to find Fibonacci Series upto given range.This C++ program use simple logic to get the concept of writing code in C++ for Fibonacci Series
Fibonacci Series program logic explanation
if part will run. let input is equal to 5
Before loop variables values
The Fibonacci numbers or Fibonacci series or Fibonacci sequence has first two numbers equal to 1 and 0 and the further each number is consist of the addition of previous two numbers1st number = 0
2nd number = 1
3rd number = 0+1= 1
4th number = 1+1= 2
5th number = 1+2= 3
And so on.
The Fibonacci Sequence can be written as 0,1,1,2,3,5,8,13........
C++ program to find Fibonacci Series upto given range.This C++ program use simple logic to get the concept of writing code in C++ for Fibonacci Series
- #include<iostream>
- using namespace std;
- int main()
- {
- int range, first = 0, second = 1, fibonicci=0;
- cout << "Enter Range for Terms of Fibonacci Sequence: ";
- cin >> range;
- cout << "Fibonicci Series upto " << range << " Terms "<< endl;
- for ( int c = 0 ; c < range ; c++ )
- {
- if ( c <= 1 )
- fibonicci = c;
- else
- {
- fibonicci = first + second;
- first = second;
- second = fibonicci;
- }
- cout << fibonicci <<" ";
- }
- return 0;
- }
Fibonacci Series program logic explanation
- Before writing C++ code we understand that what is Fibonacci Series and in what sequence numbers are occurs
- After understanding mathematically Fibonacci series completely we thought it in the form of C++ code
- The Fibonacci series is infinite but we can't make a program that display an infinite output
- In above program we take input the range in integer upto which Fibonacci series will be displayed
- For this purpose a for loop has been taken which starts from 0 and terminates less than range for example if Input is 5 then for loop will run from 0 to 4.
- In for loop if variable 'c' is less or equal than 1 in this case if statement will be executed and if 'c' is greater than 1 else part will be executed for greater than 1
if part will run. let input is equal to 5
Before loop variables values
first = 0, second = 1, Fibonacci=0;
After input range=5
Values are changing in the following sequence
After input range=5
Values are changing in the following sequence
in below table ' c ' representing the for loop iterations
c first second Fibonacci Output
0 0 1 0 0
1 0 1 1 0 1
2 0 1 0+1=1 0 1 1
1 1 1
3 1 1 1+1=2 0 1 1 2
1 2 2
4 1 2 1+2=3 0 1 1 2 3 its final output
2 3 3
Image View Of Program (Click on Image To Enlarge)
Fibonacci series in C++ image Code
c first second Fibonacci Output
0 0 1 0 0
1 0 1 1 0 1
2 0 1 0+1=1 0 1 1
1 1 1
3 1 1 1+1=2 0 1 1 2
1 2 2
4 1 2 1+2=3 0 1 1 2 3 its final output
2 3 3
Image View Of Program (Click on Image To Enlarge)
Fibonacci series in C++ image Code
Fibonacci series C++ Code |
If you have any queries comment below.
Easier way-(found it out on my own and it works perfectly)
ReplyDeleteint a=0,b=1 ;
for(i=1; i<=n-1; i++)
{
cout<<b ;
b=b+a ;
a=b-a
}
Excellent! you are really genius...
DeleteWOW!
DeleteYou are genius!
for(i=1; i<=n-1; i++)
Deleteu dnt declare n,
why usw it??
good!!!
ReplyDeleteGood job! . You can also use i<n instead of your condition statement. it will be same out put. It prints Fibonacci using two variables.
ReplyDeletegreat work
ReplyDeletethanks boss
ReplyDeleteExellent!!! it will help in my exams, so very thanx to you... i want yor contact oe skype id..
ReplyDeleteits easy
ReplyDeleteBoth ov U had a great work...
ReplyDeleteRudraneel Das..
ReplyDeleteThere is some minor errors in your code...
keep it in mind Fibonacci Series starts from ZERO...
but your logic is better...
the right code without any error...
#include
#include
void main()
{
clrscr();
int i,n,a=0,b=1;
cout<<"Enter Range for Fabnocci Series:";
cin>>n;
cout<<a<<" ";
for(i=1;i<n;i++)
{
cout<<b<<" ";
b=b+a;
a=b-a;
}
}
Write the output for the following program: 3
ReplyDeleteint main()
{
clrscr();
int a[] = {1,2,3,4,5};
int l, *k;
k = &a[0];
for (l=0;l<5;l++)
{
cout<<*k<<”\t”;
k++;
}
return 0;
}
C++ Program to Generate Fibonacci series
ReplyDeleteFibonacci Series is in the form of 0, 1, 1, 2, 3, 5, 8, 13, 21,...... To find this series we add two previous terms/digits and get next term/number.
Thanks for this amazing post.This program Fibonacci Series is very helpful for me.
ReplyDeletecan someone plz tell the code if the range is between 100 to 500...i mean if the start of range is not always zero?
ReplyDelete