The Natural Logarithm can be approximated by the following series:
(x – 1 / x) + 1/2 (x – 1 / x)2 + 1/2 (x – 1 / x)3 + 1/2 (x – 1 / x)4 + ..
If x is input through the keyboard, write a C program to calculate the sum of first seven terms of this series.
Related Read:
For Loop In C Programming Language
Basic Arithmetic Operations In C
Video Tutorial: C Program To Calculate Sum of First 7 Terms of Natural Logarithm
#include<stdio.h> #include<math.h> int main() { int count; float x, result = 0.0; printf("Enter value of x\n"); scanf("%f", &x); for(count = 1; count <= 7; count++) { if(count == 1) { result = (x - 1) / x; } else { result = result + pow( (x - 1) / x, count) * 0.5; } } printf("Result of first 7 terms = %0.2f\n", result); return 0; }
Output 1
Enter value of x
5
Result of first 7 terms = 1.98
Output 2
Enter value of x
14
Result of first 7 terms = 3.10
According to the problem statement it is clear that we need sum of first 7 terms of Natural Logarithm. So we initialize the loop counter variable count to 1 and iterate through the for loop until count value is less than or equal to 7.
Inside for loop we calculate the value of first 7 terms in the series and store it inside variable result.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert