NUMBER OF RUNS

What is meant by the number of runs here are for example only the requested rate is 3series. Then calculated to be:
1 - 1 / 3 + 1 / 5 - 1 / 7
Note that in the calculation process is the + and - changes every time a process ofcalculation. Okay, now we discuss the code:


#include <iostream>
using namespace std;

void main () {
      int mp = -1;
      double satu = 1, ulang = 3;
      int masuk;
      cout << "masukan nilai : ";
      cin >> masuk;
     
      for (int ulang2 = 1; ulang2 <= masuk; ++ulang2) {
            satu = satu + ((1 / ulang) * mp);
            mp *= -1;
            ulang += 2;
      }

      cout << satu << "\n";
}


logic:
Doing the calculation of the first tribe to tribe to - n (number of terms requested by theuser). Perhaps the most "disturbing" your thinking is how to make the + sign and minuschange - change every time, right? Though it could have been 
solved simply by multiplying -1 to -1 so the results can be positive, right? If it were so,"affairs" following the wrong .
Explanation of code:
The above code is also quite clear if observed properly. Initially we made ​​some "fruit" of variables:

 What will be the input to / from the user, the incoming
 Who will "accommodate" the results of calculations, ie one
 What will be the divisor and continue to be raised in value, which is re-
 Who will "change" signs and the type of calculation (from the sum to be reduced andvice versa), the mp

Then we do the loop by adding a variable one that is worth 1 (initially) with the results ofa variable divided by the first birthday where the value is 3, and then add 2 for eachiteration is done then multiplied by the MP variable value -1 at the beginning so that yieldcalculation: 1 - (1 / 3). variable mp then multiplied by -1 to yield positive values ​​so thatthe next calculation to be as follows:
1 - (1 / 3) + (1 / 5)
And so on until the calculation reaches the tribe to - n, and the calculation process wasdone once again and stopped. The result is then indicated by a single variable.Obviously, right?

Leave reply

Back to Top