Skip to content

Program Fibonanci

Oktober 19, 2010

ni loh contoh program fibonanci sapa tau bermanfaat tuk sohib2 ……

#include <iostream.h>

class fibonacci {
friend istream& operator>>(istream&, fibonacci&);
friend ostream& operator<<(ostream&, fibonacci&);
public:
int hitung(int);
void cetak(int);
private:
int suku_n;
};

int fibonacci::hitung(int n) {
if (n==1 || n==2) return 1;
else return hitung(n-1)+hitung(n-2);
}

void fibonacci::cetak(int n) {
if (n>0) {
cetak(n-1);
cout << hitung(n) <<” “;
}
}

istream& operator>>(istream& input, fibonacci& x) {
cout << “Masukkan integer : “;
input >> x.suku_n;
return input;
}

ostream& operator<<(ostream& output, fibonacci& x) {
output << “Jumlah Deret : \n”;
x.cetak(x.suku_n);
return output;
}

void main() {
fibonacci Bilangan;
cin >> Bilangan;
cout << Bilangan << endl;
}

Coment atuh….?

From → Latihan

2 Komentar
  1. jawad gmna maksudnya setelah aku kompail ga jalan programnya,,,,,,
    bnerin atu,,,,,,

    • ni yg bsa d kompail mas ical…
      tp blum sempurna tlong donk bantu2 ya…….?

      #include

      template

      class Array1D{
      friend ostream& operator<<(ostream&,const Array1D&);
      public:
      Array1D(int size=0);
      Array1D(const Array1D& v);
      Array1D(){delete[]element;}
      T& operator[](int i)const;
      int Size(){return size;}
      Array1D& operator =(const Array1D& v);
      Array1D operator+()const;
      Array1D operator+(const Array1D& v)const;
      Array1D operator-()const;
      Array1D operator-(const Array1D& v)const;
      Array1D operator*(const Array1D& v)const;
      Array1D&operator+=(const T& x);
      Array1D& ReSize(int sz);
      Array1D& geser_kanan();
      Array1D& geser_kiri();
      private:
      int size;
      T*element;
      };
      template
      Array1D::Array1D(int sz)
      {
      size =sz;
      element=new T[sz];
      }
      template
      Array1D::Array1D(const Array1D& v)
      {
      size = v.size;
      element=new T[size]; // get space
      for (int i=0;i<size;i++)// copy elements
      element[i]=v.element[i];
      }
      template
      T& Array1D::operator[](int i)const
      {
      return element[i];
      }
      template
      Array1D&Array1D::operator =(const Array1D& v)
      {
      if (this !=&v){
      size=v.size;
      delete[]element;
      element=new T[size];
      for(int i=0;i<size;i++)
      element[i]=v.element[i];
      }
      return *this;
      }
      template
      Array1DArray1D::operator+(const Array1D& v)const
      {
      Array1Dw(size);
      for(int i=0; i<size;i++)
      w.element[i]=element[i]- v.element[i];
      return w;
      }
      template
      Array1DArray1D::operator-(const Array1D& v)const
      {
      Array1Dw(size);
      for(int i=0; i<size;i++)
      w.element[i]=element[i]+ v.element[i];
      return w;
      }
      template
      Array1DArray1D::operator-()const
      {
      Array1Dw(size);
      for(int i=0; i<size;i++)
      w.element[i]=-element[i];
      return w;
      }
      template
      Array1DArray1D::operator*(const Array1D& v)const
      {
      Array1Dw(size);
      for(int i=0; i<size;i++)
      w.element[i]=element[i]* v.element[i];
      return w;
      }
      template
      Array1D&Array1D::operator+=(const T& x)
      {
      for(int i=0; i<size;i++)
      element[i]+=x;
      return *this;
      }
      template
      ostream& operator<<(ostream& out,const Array1D& x)
      {
      for(int i=0; i<x.size;i++)
      out<<x.element[i]<<" ";
      return out;
      }
      template
      Array1D&Array1D::ReSize(int sz)
      {
      delete[]element;
      size=sz;
      element=new T[size];
      return *this;
      }
      template
      Array1D&Array1D::geser_kanan()
      {
      int n = size;
      int temp = element[n-1];
      for (int i=n-1; i>=0; i–)
      element[i+1]=element[i];
      element[0]=temp;
      }
      template
      Array1D&Array1D::geser_kiri()
      {
      int n=size;
      int temp = element[0];
      for(int i=0; i<n; i++)
      element[i]=element[i+1];
      element[n-1]=temp;

      }

      int main()
      {
      int X(10),Y,Z;
      for (int i=0; i<10; i++)
      X=i;
      cout<<" X = "<< X <<endl;
      cout<<" X is "<< X <<endl;
      Y=X;
      cout<<" Y is "<< Y <<endl;
      Y += 2;
      cout << " X incremented by 2 is " <<X<<endl;
      Z=(Y+X)*Y;
      cout<<"(Y+X)*y is " << Z <<endl;
      cout<< "-(Y+X)*Y is " << -Z <<endl;
      cout<<"setelah digeser kekanan :"<<(Y-X)*Y<<endl;
      cout<<"setelah digeser kekiri :"<<(Y+X)<<endl;
      system("PAUSE");
      }

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: