C Program To Calculate Sum of First 7 Terms of Natural Logarithm

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



YouTube Link: https://www.youtube.com/watch?v=8mVIyfub9o4 [Watch the Video In Full Screen.]

Source Code: C Program To Calculate Sum of First 7 Terms of Natural Logarithm

  1. #include<stdio.h>  
  2. #include<math.h>  
  3.   
  4. int main()  
  5. {  
  6.     int count;  
  7.     float x, result = 0.0;  
  8.   
  9.     printf("Enter value of x\n");  
  10.     scanf("%f", &x);  
  11.   
  12.     for(count = 1; count <= 7; count++)  
  13.     {  
  14.         if(count == 1)  
  15.         {  
  16.             result = (x - 1) / x;  
  17.         }  
  18.         else  
  19.         {  
  20.             result = result + pow( (x - 1) / x, count) * 0.5;  
  21.         }  
  22.     }  
  23.   
  24.     printf("Result of first 7 terms = %0.2f\n", result);  
  25.   
  26.     return 0;  
  27. }  

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

Logic To Calculate Sum of First 7 Terms of Natural Logarithm

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