Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n!.



 GTU Programming for Problem Solving Practical-27


// 27. Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n!.

#include <stdio.h>

float fact(float a);

int main(void)
{
    float n, sum = 0;
    printf("\nEnter the number till ypu wanted series");
    scanf("%f", &n);
    printf("\n0");

    for (float i = 1; i <= n; i++)
    {
        printf(" + 1/%.0f!", i);
        sum = sum + (1 / fact(i));
    }
    printf(" = %f", sum);

    return 0;
}
float fact(float a)
{
    float ans = 1;

    for (int i = 1; i <= a; i++)
    {
        ans = ans * i;
    }
    return ans;
}

Output:

Enter the number till ypu wanted series10

0 + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! + 1/6! + 1/7! + 1/8! + 1/9! + 1/10! = 1.718282

Post a Comment

0 Comments