C++ | Sololearn: Learn to code for FREE!
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 🖤😀

18th Dec 2018, 8:24 PM
Waleid Al-Shhadat
2 Answers
18th Dec 2018, 9:07 PM
AKS
AKS - avatar
+ 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).
18th Dec 2018, 9:07 PM
John Wells
John Wells - avatar