0
What is C++ program to calculate the sum of the series 1-1/2+1/3+ . . . +1/999
3 Antworten
+ 1
https://code.sololearn.com/ceAt4JOz943T
It's called a harmonic series. Sometimes a number like 999 doesn't work right in the online app because of server limitations, but if you copy the code into a local compiler on your computer it should work fine.
0
But it's not working bro!! I tried many times
0
#include <iostream>
#include <sstream>
double harmonicSeries(int pTerms)
{
double result;
for (int i = 1; i <= pTerms; i++)
{
result += (double)1 / (double)i;
result=result*(-1.0);
}
return result;
}
int main() {
std::cout << std::fixed << harmonicSeries(999);
return 0;
}
Here is the code. Also thanks to @MrSkee, i just modified his code.
Also my name is Vaibhav.