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 https://www.youtube.com/watch?v=8mVIyfub9o4]

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

#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

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

C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!

Lets write a C program to add first seven terms of the following series 1 / 1! + 2 / 2! + 3 / 3! + ….. + 7 / 7!

We’ve also written the C program to ask the user to enter the number of terms of the series that has to be added. You can find code to both of it below.

Related Read:
For Loop In C Programming Language
while loop in C programming
C Program To Find Factorial of a Number using For Loop

C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!


[youtube https://www.youtube.com/watch?v=Nok-OqhKpDY]

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


Source Code: C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + 7/7!

 

int main()
{
    int num = 1, count;
    float sum = 0.0, fact;

    while(num <= 7)
    {
        fact = 1;
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        sum = sum + (num / fact);

        num++;
    }

    printf("Sum of series is %f\n", sum);

    return 0;
}

Output:
Sum of series is 2.718056

In above code, while loop iterates from 1 to 7. For each iteration for loop calculates the factorial of the selected number – which is present in variable num. Outside for loop, we add the individual series elements. At the end of while loop execution, variable sum will have sum of 7 terms of the series.

Source Code: C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!

 

int main()
{
    int num = 1, count, limit;
    float sum = 0.0, fact;

    printf("Enter the number of terms\n");
    scanf("%d", &limit);

    while(num <= limit)
    {
        fact = 1;
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        sum = sum + (num / fact);

        num++;
    }

    printf("Sum of %d terms of series is %f\n", limit, sum);

    return 0;
}

Output 1:
Enter the number of terms
7
Sum of 7 terms of series is 2.718056

Output 2:
Enter the number of terms
8
Sum of 8 terms of series is 2.718254

In above code, we ask the user to enter the number of terms in the series that needs to be added. While loop iterates until the user entered number of times. Inside for loop we calculate the factorial of the selected number(number is selected by while loop and it’ll be present in variable num). Outside the for loop we add the individual series element. At the end of execution of while loop we’ll have sum of the series until user entered number of terms in the series.

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