Slip 2 - B) Write a program to calculate sum of following series up to n terms. Sum=X+X^2 /2!+X^3 /3!+......

Solution:

#include<math.h>
#include<stdio.h>
    int fact(int index)
    {
    int f = 1, i;
    for(i = 1; i <= index; i ++)
    {
        f = f*i;
    }
    return f;
    }
// Driver Code
void main()
{
    int x = 1;
    int n = 2;
    double sum = 0,   m;
    // Sum of n-1 terms starting from 2nd term
    int i;
    for (i = 1; i < =n; i++) {
    m =  pow(x, i) / fact(i);
    sum = sum + m;
   }
    printf("\n%.4f", sum);
    getch();
}

Post a Comment

0 Comments