suku ke n dari deret fibonacci diperoleh dengan rumus:
fibonacci(n)=fibonacci(n-1) + fibonacci(n-2)
dengan nilai awal untk n=1 dan n=2 berharga 1
fungsi fibonacci (input n;integer) :integer
deskripsi
if(n=1) or (n=2) then fibonacci =1
else fibonacci =fibonacci(n-1)+fibonacci(n-2)
endif
#include <conio.h>
class Operator {
friend ostream& operator<<(ostream&, Operator&);
friend istream& operator>>(istream&, Operator&);
public:
long fibonacci(int);
private:
int n;
};
int fibonacci(int n)
{ if((n==1)||(n==2)) return(1);
else return(fibonacci(n-1)+fibonacci(n-2));
}
ostream& operator<<(ostream& out, Operator& a){
for (int i=1;i<=a.n;i++)
out<<fibonacci(i)<<" ";
return out;
}
istream& operator>>(istream& in, Operator& a){
cout<<"Sampai suku ke:";
in>>a.n;
return in;
}
main(){
Operator run;
cin>>run;
cout<<run;
getch();
}
Tidak ada komentar:
Posting Komentar