According to the task, it was necessary to find the sum of the series ((-1) ^ (n-1) x ^ (2n-1)) / ((4 ^ 2) (2n-1) That’s how it happened. But for k = 2 and 3 and x = 2 and 3 the sum of the series is 0. Could you look and say if something is wrong somewhere?

#include <conio> #include <iostream> #include <cmath> using namespace std; int main() { long Sum=0; double n=1; int k; double x; char answer; answer = 'y'; while (answer =='y') { cout << "vvedite k : "; cin >> k; cout << "vvedite x : "; cin >> x; for (Sum=0 , n=1 ; n <= k ; n++ ) { Sum +=( pow ( -1.0 , n-1 )*pow ( x , 2.0*n-1))/ (pow ( 4.0 , n) *(2.0*n-1)); } cout << "Summa = " << Sum << endl; cout << "Pos4itat' ewe raz? y/n\n"; cin >> answer; } getch(); return 0; } 
  • Offtopic: in C ++, it is not necessary to define all variables at the beginning of a function. On the contrary, it is considered to be good practice to define a variable as close as possible to the first use. - dzhioev

1 answer 1

Because for some reason you write the answer in long, while the result is a double. When the value of k <= 3, the sum is less than one, therefore when casting to long you get a zero. And also, do not write in transliteration. Learn English better