New course! Every coder should learn Generative AI!
Try a free lesson+ 2
C++
how can i write this function code ?? ln(1+x)= x-(x^2/2)+(x^3/3)-(x^4/4)+....+(-1)^n * x^n/n i will be happy if you are helping me 🖤😀
2 Answers
+ 9
May this help you 😥
https://code.sololearn.com/csjBb947RVg9/?ref=app
+ 4
The easiest is using a recursive function (ln). It needs to stop recursing when n becomes 1 returning x. Otherwise, if n is odd, calculate x^n/n and return that plus the recursive call of ln(x, n-1). Otherwise, it is even so calculate -(x^n/n) and return that plus the recursive call of ln(x, n-1).
An n of 2 would have two recursions. The outer ln(x, 2) and the inner ln(x, 1). The inner would return x to the outer. The outer would calculate and return ln(x, 1)-(x^2/2).