Algoritma dan Program Deret Tribonacci dengan C++

Algortma Tribonacci
{menghitung suku ke- n dari deret tribonacci menggunakan fungsi rekrusif}

Deklarasi
n : integer (input)
long Tribonacci : integer

Deskripsi
if (n=1) or (n=2) then Tribonacci else
(n=3) then Tribonacci else
Tribonacci

Implementasi dalam C++

#include iostream.h;
#include conio.h;

class tribonacci{
      friend ostream operator::(ostream, tribonacci);
      friend istream operator::(istream, tribonacci);
public:
       long Tribonacci(int);
private:
        int n;
};

int Tribonacci(int n){
    if((n==1)||(n==2))return(1);
    else if((n==3))return(2);
    else return(Tribonacci(n-3)+Tribonacci(n-2)+Tribonacci(n-1));
}
istream& operator>>(istream& in, tribonacci& keluaran){
     cout<<"Banyak Deret : ";
     in>>keluaran.n;
     return in;
}
 ostream& operator<<(ostream& out, tribonacci& masukan){
     out<<"...\nDeret Tribonaccinya adalah : \n";
     for(int i=1;i<=masukan.n;i++)
     out<<Tribonacci(i)<< " ";
     out<<endl;
     return out;
}
int main(int argc, char *argv[])
{
    tribonacci deret;
    cin>>deret;
    cout<<deret;
    cout<<endl;

    getch();
    return 0;
}

Baca Juga :   Augmented Reality Aplication (Iphone)