Algoritma :
Deklarasi
n : integer
rumus,jumlah,total : float
Deskripsi
jumlah = 0;
total = 0;
rumus = -1;
for j <= 1 to n do
rumus =(rumus*(-1));
total = rumus/j;
jumlah += total;
if (j==1)
write(total)
if (j>1)
then
write(+ total)
endif
then
write (jumlah)
endfor
Berikut programnya :
#include <iostream.h>
class 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();
return 0;
}
Explanation:
The program above is used to calculate the number of runs yatiu 1-1 / 2 + 1 / 3 - 1 / 4 +...+ 1 / n by using a recursive function which will call itself the return amount;. In this program using for loop and if the election. The process of the program will execute the first value of j = 1 as initial value. After that will run the formula specified in the program and then conduct the election by using if that is if (j == 1) then the total will appear and if (j> 1) will add the total. Order number appears the program will call itself the return number; such as the above program.
The program above is used to calculate the number of runs yatiu 1-1 / 2 + 1 / 3 - 1 / 4 +...+ 1 / n by using a recursive function which will call itself the return amount;. In this program using for loop and if the election. The process of the program will execute the first value of j = 1 as initial value. After that will run the formula specified in the program and then conduct the election by using if that is if (j == 1) then the total will appear and if (j> 1) will add the total. Order number appears the program will call itself the return number; such as the above program.
0 komentar:
Posting Komentar