C++ :
#include <iostream>
#include<conio.h>
#include<math.h>
using namespace std;
class hitung
{
public:
void input();
int proses();
private:
int a;
float bil,hasil,total;
};
void hitung::input(){
cin>>a;
cout<<endl;}
int hitung::proses(){
hasil=0;
total=0;
bil=-1;
for(int j=1; j<=a; j++){
bil=(bil*(-1));
total=bil/j;
hasil+=total;
if(j==1)
cout<<"("<<bil<<"/"<<j<<")";
if(j>1)
cout<<" +("<<bil<<"/"<<j<<")";
}
cout<<endl<<endl<<"hasil penjumlahan deret = "<<hasil;
return hasil;
}
int main()
{
cout<<"menghitung jumlah Hasil Deret 1-(1/2)+(1/3)-(1/4)+...+(1/n)"<<endl<<endl;
cout<<"masukan nilai n : ";
hitung deret;
deret.input();
deret.proses();
getch();
return 0;
}
Output :
Raptor :