Kamis, 17 November 2011

C + + program to find the sum of the series 1-1/2 +1/3-1 / 4 +...+ 1 / n

  Agus Nur Ikhsan       Kamis, 17 November 2011
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)
end for
in program :
#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<<"tampilan hasil penjumlahan deret = "<<jumlah;
     return jumlah;
     }

     int main()
     {
     cout<<" menghitung jumlah dari 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.
Among the explanation of our group
logoblog

Thanks for reading C + + program to find the sum of the series 1-1/2 +1/3-1 / 4 +...+ 1 / n

Previous
« Prev Post

Tidak ada komentar:

Posting Komentar