oke gaan..kmaren kan udah kita membahas tentang penghitungan interatif..
sekarang saya kasih yang ITERATIF gaan....oke oke....
coba tengok programnya niee...
#include < cstdlib >
#include < iostream >
using namespace std;
class hitung {
public:
void masuk();
float proses();
void keluar();
private:
int n;
float rumus,jumlah,total;
};
void hitung::masuk(){
cout << "program sederhana menghitung jumlah dari rumus 1-(1/2)+(1/3)-(1/4)+...+(1/n)" << endl << endl; cout << "masukkan nilai n : ";
cin >> n;
cout << endl; }
float hitung : : proses(){
jumlah=0; total=0; rumus=-1;
for(int i=1; i < =n; i++){
rumus=(rumus*(-1));
total=rumus/i; jumlah + = total;
if(i == 1) cout << "(" << total << ")"; if(i > 1) cout << "+(" << total << ")";
}
return jumlah;
}
void hitung::keluar(){
cout << "\nhasil penjumlahan deret = " << jumlah << endl << endl;
}
int main(int argc, char *argv[])
{
hitung x;
x.masuk();
x.proses();
x.keluar();
system("PAUSE");
return EXIT_SUCCESS;
}
sekarang saya kasih yang ITERATIF gaan....oke oke....
coba tengok programnya niee...
#include < cstdlib >
#include < iostream >
using namespace std;
class hitung {
public:
void masuk();
float proses();
void keluar();
private:
int n;
float rumus,jumlah,total;
};
void hitung::masuk(){
cout << "program sederhana menghitung jumlah dari rumus 1-(1/2)+(1/3)-(1/4)+...+(1/n)" << endl << endl; cout << "masukkan nilai n : ";
cin >> n;
cout << endl; }
float hitung : : proses(){
jumlah=0; total=0; rumus=-1;
for(int i=1; i < =n; i++){
rumus=(rumus*(-1));
total=rumus/i; jumlah + = total;
if(i == 1) cout << "(" << total << ")"; if(i > 1) cout << "+(" << total << ")";
}
return jumlah;
}
void hitung::keluar(){
cout << "\nhasil penjumlahan deret = " << jumlah << endl << endl;
}
int main(int argc, char *argv[])
{
hitung x;
x.masuk();
x.proses();
x.keluar();
system("PAUSE");
return EXIT_SUCCESS;
}


Posting Komentar