Friday, April 6, 2012

Program 5TH in C++

PROBLEM STATEMENT :

WAP TO FIND NTH FIBONACCI NUMBER

METHOD 1 : RECURSIVE METHOD


#include <iostream.h>
#include <cstdio>
#include <unistd.h>
#include <cstdlib>

using namespace std;

//funtion to generate nth fibonacci number

int findF(int n)
{
if (n-1 > 2)
{

return findF(n-1)+findF(n-2);
}
else if(n-1 > 0)
{
return 1;
}
else
{
return 0;
}

}
int main()
{

int position=0;
int val=0;

cout<<"\n\nWhich no in fibonacci series you want to generate : \n\n";

//loop for entering correct value with error checking.

while(true)
{
cout<<"Enter : ";
cin>>position;

if(cin.good() && position > 0)
{
break;
}
else
{
cout<<"\n\n Enter positive integer only\n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
continue;
}
}

//finding required value;

val=val+findF(position);
cout<<"\n\nThe number at position "<<position<<" in the fibonacci series is : "<<val;
cout<<endl<<endl;
}

METHOD 2 : ITERATIVE METHOD


#include <iostream.h>
#include <cstdio>
#include <unistd.h>
#include <cstdlib>

using namespace std;

int main()
{

int i=1;
int first=0;
int second=1;
int third=1;
int position=1;

cout<<"\n\nEnter the position whose value you want to print in the fibonacci series : \n\n";

//loop to enter integer value with error checking

while(true)
{
cout<<"Enter : ";
cin>>position;

if(cin.good() && position>0)
{
break;
}
else
{
cout<<"\n\nEnter only positive integer \n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
continue;
}
}

//loop to generate nth fibonacci number

while(i < position-1)
{
third=first+second; //next number is sum of previous two
first=second; //first is set to second
second=third;//second is set to third
i++;
}

cout<<"\n\nThe number at position "<<position << " in the fibonacci series is : " <<third <<endl << endl;

}

METHOD 3 : MATRIX METHOD


#include <iostream.h>
#include <cstdio>
#include <unistd.h>

using namespace std;

void power(int [][2], int);
void multiply(int [][2], int [][2]);

int fib(int n)
{

if(n == 1)
{
return 0;
}

int m[2][2]={{1,1},{1,0}};
power(m,n-2);
return m[0][0];

}
void power(int m[2][2],int n)
{

if( n==1 || n==0)
{
return;
}
int p[][2]={{1,1},{1,0}};

power(m,n/2);
multiply(m,m);

if( n%2 != 0)
{
multiply(m,p);
}

}

void multiply(int m[2][2], int n[2][2])
{

int w=m[0][0]*n[0][0]+m[0][1]*n[1][0];
int x=m[0][0]*n[0][1]+m[0][1]*n[1][1];
int y=m[1][0]*n[0][0]+m[1][1]*n[1][0];
int z=m[1][0]*n[0][1]+m[1][1]*n[1][1];

m[0][0]=w;
m[0][1]=x;
m[1][0]=y;
m[1][1]=z;

}

int main()
{

int position=0;
cout<<"\n\nEnter the position whose value you want to print in the fibonacci series : \n\n";

while(true)
{
cout<<"\n\nEnter : ";
cin>>position;
if(cin.good() && position > 0)
{
break;
}
else
{
cout<<"\n\nEnter only positive integers \n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cin.sync();

}

}

int value = fib(position);
cout < <"\n\n The number at position " < <position < <" in the fibonacci series is : " < <value < <endl < <endl;
}

4 comments:

  1. Well Tried garima.. But there is a simple way also.

    Golden Ratio is the ratio between two successive Fibonacci series number. Using this property we could directly find F(n) and avoid such huge computation.
    http://en.wikipedia.org/wiki/Golden_ratio#Relationship_to_Fibonacci_sequence

    ReplyDelete
    Replies
    1. Thank you...

      Please clear my doubt

      golden ratio 1.6180339.. = F(n+1)/F(n)

      for finding F(n) we need F(1)* (golden ratio)^(n-1)

      hows that goin to give exact result??

      Delete
  2. Garima,
    You could use simple floor function for it.

    Please check this page :

    http://en.wikipedia.org/wiki/Fibonacci_number#Computation_by_rounding

    ReplyDelete