Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Friday, 20 March 2015

In this article i will  write a program to generate Fibonacci series/sequence up to the given limit using in C++ language using For loop.
Fibonacci series: In Fibonacci series, first number is 0 and second number is 1 and further next number is the sum/addition of previous two numbers.
 0 1 1 2 3 5 8 13 21 34 55....... this is the example of Fibonacci series.

Explanation:
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...

 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
Categories: ,

0 comments:

Post a Comment