sequence: 1, 3, 7, 15, 31,

in this post I will keep looking for a programming syntax to create a function Fibonacci numbers iteratively

Her following source:

#include <iostream.h>
#include <conio.h>
class Deret {
friend istream& operator>>(istream&, Deret&);
friend ostream& operator<<(ostream&, Deret&);
public:
void cetak();
private:
int bil;
int i, k, sum;
};

void Deret::cetak() {
sum=0;
k=0;
for (i=1; k<=bil;i++){
if (k <= bil && (k+2)>=bil)
break;
k=(i*3)-2;
sum+=k;
cout << “Suku ke : ” << i << ” = ” << sum << endl;
}
}
istream& operator>>(istream& input, Deret& x) {
cout << “Masukkan integer : “;
input >> x.bil;
return input;
}

ostream& operator<<(ostream& output, Deret& x) {
output << “Jumlah Deret : “;
x.k=0;
for (x.i=1; x.k<= x.bil; x.i++){
x.k=2*(x.i-1)+1;
if (x.k <= x.bil && (x.k+2)>=x.bil){
cout << x.k<< ” adalah ” << endl;
break;
}
else cout << x.k << ” + “;
}
x.cetak();
return output;
}

int main() {
Deret Bilangan;
cin >> Bilangan;
cout << Bilangan << endl;
getch();
return 0;
}

Please give it a try🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: