Menghitung deret dengan iteratif

#include <iostream.h>

class hitung{
friend istream& operator>>(istream&, hitung&);
friend ostream& operator<<(ostream&, hitung&);
public:
int proses();
void input();
private:
int n;
float rumus,jumlah,total;
};

void hitung::input()
{
cin>>n;
cout<<endl;
}

int hitung::proses()
{
jumlah=0;
total=0;
rumus=-1;
for(int j=1; j<=n; j++)
{
rumus=(rumus*(-1));
total=rumus/j;
jumlah+=total;
if(j==1)
cout<<“(“<<total<<“)”;
if(j>1)
cout<<“+(“<<total<<“)”;
}
cout<<endl<<endl<<“hasil penjumlahan deret = “<<jumlah;
return jumlah;
}

int main()
{
cout<<“program sederhana menghitung jumlah dari rumus 1-(1/2)+(1/3)-(1/4)+…+(1/n)”<<endl<<endl;
cout<<“tentukan nilai n : “;
hitung deret;
deret.input();
deret.proses();

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