Program Deret Fibonacci dengan C++

#include <cstdlib>
#include <iostream>

using namespace std;
class fibonacci{
friend istream& operator>>(istream&, fibonacci&);
friend ostream& operator<<(ostream&, fibonacci&);
public:
fibonacci();
void proses();
void jumlah();
private:
int x[100];
int a,hasil;
};
istream& operator>>(istream& in, fibonacci& masukan){
cout<<“Masukkan Banyak Suku :”;
in>>masukan.a;
}
ostream& operator<<(ostream& out, fibonacci& keluar){
out<<“Suku Fibonacci :”<<keluar.a<<endl;
out<<“Deret Fibonacci :”;
for(int i=0; i<keluar.a; i++){
out<<keluar.x[i]<<“,”;
}
out<<endl;
out<<“Hasil Jumlah Deret Fibonacci :”<<keluar.hasil<<endl;
}
fibonacci::fibonacci(){
cout<<“\t\t<< PROGRAM MENCETAK DAN MENGHITUNG DERET FIBONACCI >>”<<endl;
}
void fibonacci::proses(){
if(a==1) hasil=1;
else if(a==2) hasil=1;
else
x[0]=1;
x[1]=1;
for(int i=2; i<a; i++){
x[i]=x[i-1]+x[i-2];
}
}
void fibonacci::jumlah(){
hasil=0;
for(int i=0; i<a; i++){
hasil=hasil+(x[i]);
}
}
int main(int argc, char *argv[])
{
fibonacci x;
cin>>x;
x.proses();
x.jumlah();
cout<<x;
system(“PAUSE”);
return EXIT_SUCCESS;
}

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