RSS

Fungsi Rekursif untuk mencari deret Aritmatika

05 Mei

#include <cstdlib>
#include <iostream>

using namespace std;
class deret{
friend ostream& operator<<(ostream&, deret&);
friend istream& operator>>(istream&, deret&);
public: float proses(int n);
private:
int n;
};

istream& operator>>(istream& in, deret& masuk){
cout<<“masukkan n : “;
in>>masuk.n;
return in;
}
ostream& operator<<(ostream& out, deret& keluar){
cout<<“hasilnya : “;
out<<keluar.proses(keluar.n);
return out;
}
float deret::proses(int n){
if(n==1){
return 1;
}
else if (n%2==0){
float a=(float)n;
return proses(n-1)-(1/a);
}
else{
float a=(float)n;
return proses(n-1)+(1/a);

}
}
int main(int argc, char *argv[])
{
deret x;
cin>>x;
cout<<x;
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

 
%d blogger menyukai ini: