Calculate the sum of following series
C program to add first N terms of the following series. Value of N is entered by the user.
1/1! + 2/2! + 3/3! + 4/4! + …….
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j;
float sum=0;
unsigned long long fact;
printf("Enter the number of terms to be added\t:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact = 1;
for(j=1;j<=i;j++)
{
fact = fact * j;
}
sum = sum + ((float)i/fact);
}
printf("Sum of first %d terms of the series ( 1/1! + 2/2! + 3/3! + ...) is\t: %f",n,sum);
getch();
}
Output
Enter the number of terms to be added :6
Sum of first 6 terms of the series ( 1/1! + 2/2! + 3/3! + ...) is : 2.716667