Week 6
6c) Write a C++ program that writes out the Fibonacci series. Find the largest Fibonacci number that fits in an int.
Aim::To display fibonacci series and to print the largest fibonacci number that fits in an int
Program::
#include< iostream>
using namespace std;
int main()
{
int i=0,n;
int fib[100]={0};
int temp[40],t=-1;
cout<<"Enter the range to print the fibonacci serries \n";
cin>>n;
for(i=0;i<100;i++)
{
if(i<2)
fib[i]=i;
else
fib[i]=fib[i-1]+fib[i-2];
if(fib[i]<0)
{
t++;
temp[t]=i-1;
}
}
cout<<"The fibonacci series is \n";
for(i=0;i< n;i++)
{
cout<< fib[i]<<"\t";
}
cout<<"The largest fibonacci number that fits in an intis f[ " << temp[0]<<"]="<< fib[temp[0]];
}
Output::
Enter the range to print the fibonacci serries 10 The fibonacci series is 0 1 1 2 3 5 8 13 21 34 The largest fibonacci number that fits in an int is f[46]=1836311903

No comments:
Post a Comment