Introduction: In this article i am going to write a program to create or generate Fibonacci series/sequence up to the given limit using in C++ language using For loop. In some of previous articles i explained the programs to Calculate factorial of a number using for loop and What is For loop and print counting up to given limit and Print table of entered number using For loop and Else if ladder to perform operation on two numbers based on operator and What is goto statement and its advantages and disadvantages with program example .
Description: In Fibonacci series, first number is 0 and second number is 1 and further next number is the sum/addition of previous two numbers.
So, Fibonacci series with first few numbers can be written as: 0 1 1 2 3 5 8 13 21 34 55.......
For example:
First Number = 0
Second Number = 1
Third Number = 0 + 1 =1
Fourth Number = 1 + 1 =2
Fifth Number = 2 + 1 =3
Sixth Number =3 + 2 = 5
Seventh Number = 5 + 3 =8 and so on...
Implementation: Let's create program to generate Fibonacci Series with For loop using C++language.
#include<conio.h>
#include<iostream.h>
int main()
{
int i,n,first=0,second=1,fibo;
clrscr();
cout<<"Enter the number of terms:
";
cin>>n;
cout<<"\nFibonacci series:\n";
cout<<first<<"\t"<<second;
for(i=0;i<n-2;i++)
{
fibo=first+second;
first=second;
second=fibo;
cout<<"\t"<<fibo;
}
getch();
return 0;
}
- Run the program using Ctrl+F9
Output:
Enter the number of terms: 10
Fibonacci Series
0 1 1 2 3 5 8 13 21 34
Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..