#Diktat Alpro Bab 5 - No.4
4. Hitunglah nilai dari :
1-(1/2)+(1/3)-(1/4)+... +(1/n)
Program:
#include <iostream>
#include<conio.h>
using namespace std;
class hitung
{
public:
void input();
int proses();
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<<"menghitung jumlah dari rumus 1-(1/2)+(1/3)-(1/4)+...+(1/n)"<<endl<<endl;
cout<<"nilai n : ";
hitung deret;
deret.input();
deret.proses();
getch();
return 0;
}
Jumat, 08 April 2016
Diktat Alpro Bab 5 - No.4
22.19
No comments
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar