welcome

Selasa, 30 Oktober 2012

fungsi bilangan Fibonaci dengan cara iteratif

berikut source codenya :)

#include <cstdlib>
#include <iostream>

using namespace std;
class fibonaci{
friend istream& operator >> (istream&, fibonaci&);
friend ostream& operator << (ostream&, fibonaci&);

public :
void iteratif();
void iteratif2();
private:
        int a,b,c,batas;
};

istream& operator >>(istream& in, fibonaci& masuk){
cout<<"Dengan menggunakan Iteratif while " <<endl;
cout<<"Masukkan batas nilai :"; in>>masuk.batas;
cout<<"Bilangan Fibonacci < " << masuk.batas <<" adalah\no,1";
}

ostream& operator << (ostream& out, fibonaci& keluar){
keluar.a=0;keluar.b=1;
while(true){
keluar.c=keluar.a+keluar.b;
if(keluar.c>keluar.batas) break;
cout<<"," <<keluar.c;
keluar.a=keluar.b;
keluar.b=keluar.c;
}
}
void fibonaci::iteratif(){
a=0;b=1;
cout<<"Bilangan Fibonacci < " <<batas <<" adalah\no,1";
do{
c=a+b;
if(c>batas) break;
cout<<"," <<c;
a=b;
b=c;
}
while(true);
}

void fibonaci::iteratif2(){
a=0;b=1;
cout<<"Bilangan Fibonacci < " <<batas <<" adalah\no,1";
for(c=1;c<batas;c++){
c=a+b;
cout<<"," <<c;
a=b;
b=c;
}
}

int main(int argc, char *argv[])
{
fibonaci x;

cout<<"Menampilkan deret bilangan fibonacci iterative ";
cout<<endl;
cin>>x;
cout<<x;
cout<<endl;
cout<<endl;

cout<<"Dengan do while " <<endl;
x.iteratif();
cout<<endl;
cout<<endl;

cout<<"Dengan For " <<endl;
x.iteratif2();
cout<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}