Wednesday, 22 April 2015

C++ Program to print Fibonacci series

Write and execute a C++ program to

  1. display the first 25 Fibonacci numbers
  2. display the Fibonacci series up to 5000.
PROGRAM

1.


#include<iostream>
using namespace std;
int main()
{
int first=0,second=1,third,i,n;
cout<<"Enter how many terms required:";
cin>>n;
cout<<"The required Fibonacci series is:\n"<<first<<"\n"<<second<<"\n";
for(i=2;i<n;i++)
{
third=first+second;
first=second;
second=third;
cout<<third<<"\n";
}
return 0;
}



OUTPUT

Enter how many terms required:25
The required Fibonacci series is:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368


2.

#include<iostream>
using namespace std;
int main()
{
int first=0,second=1,third,i,n;
cout<<"Enter number upto which series is required:";
cin>>n;
cout<<"The required Fibonacci series is:\n"<<first<<"\n"<<second<<"\n";
third=first+second;
while(third<=n)
{
cout<<third<<"\n";
first=second;
second=third;
third=first+second;
}
return 0;
}


OUTPUT

Enter number upto which series is required:5000
The required Fibonacci series is:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181

No comments :

Post a comment

Related Posts Plugin for WordPress, Blogger...