Example 25: Natural Logarithm Series

Learn how to print a logarithmic series.

Problem

The natural logarithm can be approximated by the following series.

x1x+12(x1x)2+13(x1x)3+14(x1x)4+....\frac{x-1}{x} + \frac{1}{2}(\frac{x-1}{x})^{2}+ \frac{1}{3}(\frac{x-1}{x})^{3}+\frac{1}{4}(\frac{x-1}{x})^{4}+....

If x is an input to a function, write a program to calculate the sum of this series’s first seven terms.

Example

Input Output
3 1.086367
2 0.692262
0 -1
-10 -1

Try it yourself

Try to solve this question on your own in the code widget below. If you get stuck, you can always refer to the solution provided.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.