The sequence is given: 1, 3/2, 5/3, 7/4, 9/5, ... It is necessary to calculate the product from 1 to n of the sequence.

Hint throw up how to solve

I've tried to solve it using the general formula, but it gives the wrong numbers as a result

cin >> n for (int i = 1; i <= n; i++){ a_i = ((2 * i) - 1) / i; result = result * a_i; } 
  • one
    You will need to master the for loop. In addition, do not forget that you need to perform a real division, not integer - MBo
  • one
    Are you kidding me??? the author, why did you choose the IT profession, if you can’t write the most elementary program and do not even try - Sanaev
  • one
    Why so much aggression? I gave my code, asked why it gives the left answers - ZeroHour

2 answers 2

In general, in the code you provide, almost everything is correct. However, you should cast the variable i to a real type ( float or double ) and initialize the variable result .

 #include <iostream> int main() { int n; std::cin >> n; float result = 1; for(int i = 1; i <= n; ++i) { result *= (2*float(i) - 1)/float(i); } std::cout << result << std::endl; return 0; } 
  • Thanks, apparently due to the fact that I forgot to lead to the real, the left gave out - ZeroHour

 // 1, 3/2, 5/3, 7/4, 9/5, 11/6 var N = 6; var n = 1, nP = 1; // numerator, numerator product var d = 1, dP = 1; // denominator, denominator product var items = []; for (var i = 0; i < N; i++) { items.push(n + "/" + d); nP = nP * n; dP = dP * d; n = n + 2; d = d + 1; } console.log(JSON.stringify(items)); console.log(nP + "/" + dP + " = " + nP / dP); 

Fraction-result reduction is left as an exercise for readers.


The "formula" in the question is correct. I guess the result variable is not initialized.

 var N = 6; var result = 1; for (var i = 1; i <= N; i++) { var a_i = ((2 * i) - 1) / i; result = result * a_i; } console.log(result); 

  • one
    Somehow all this is not like c ++ - αλεχολυτ
  • @ älёxölüt hmm, and braces, like, curly - Igor
  • one
    I did not understand you about brackets, but it is somewhat strange to give an answer in a language that does not correspond to the question marks. - αλεχολυτ