luc037.c (733B)
1 /* The natural logarithm can be approximated by the following series. 2 (x-1)/x + 1/2 ((x-1)/x)^2 + 1/2 ((x-1)/x)^3 + 1/2 ((x-1)/x)^4 + ... 3 If x is input through the keyboard, write a program to calculate the 4 sum of the first seven terms of this series. */ 5 /* Let Us C, Chap- 6, Page - 106, Qn No.: B(d) */ 6 7 #include <stdio.h> 8 #include <math.h> 9 10 double series(double x) // made this fn only for fun, making a fn was not necessary 11 { 12 double result = (x - 1) / x; 13 int i; 14 for (i = 2; i <= 7; i++) 15 { 16 result += 0.5 * pow(((x - 1) / x), i); 17 } 18 return result; 19 } 20 21 int main() 22 { 23 double x; 24 printf("Enter the value for x : "); 25 scanf("%lf", &x); 26 printf("\nResult : %g", series(x)); 27 return 0; 28 }