DERET
#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;
}
#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;
}