algoritma dan program untuk menghitung deret.doc

Post on 28-Apr-2015

62 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Algoritma dan Program untuk menghitung deretS = 1/2+2/4-3/8+4/16+5/32-6/64+...+...-...

Algoritma DeretDeklarasi i,p,q,n,tanda: integer S : riilDeskripsi iß1 Sß0 input(n) while (i<=n) do if(imod3=0)then

tanda ß -1 else

tanda ß 1 endif pßi q ß2^i SßS+(p/q*tanda) ißi+1 endwhile output(S)

#include <iostream.h>#include <math.h>

main(){ int tanda,p,q,n,i; float S=0;

cout << " n = "; cin >> n; cout << " S = "; i=1; while (i<=n) {

p = i; q = pow(2,i);

if (i%3) {

tanda = -1;

cout << "+"<<p << "/" << q ; } else {

tanda = 1; cout << "-"<<p << "/" << q ;

}

S = S + (tanda*((1.0*p)/q)); i++;

} cout << "\n S = " << S;

}

top related