Slip 18 - B) Write a program to calculate sum of following series up to n terms. Sum=X-X 2 /2!+X3 /3!-...... (Note: Write separate user defined function to calculate power and factorial)

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;
    }
void main()
{
    int x = 1;
    int n = 3;
    double sum = 0,term=-1,   m;
    // Sum of n-1 terms starting from 2nd term
    int i;
    for (i = 1; i <= n; i++) {

        term = term * (-1);
    m =  term * pow(x, i) / fact(i);
    sum = sum + m;
    }
    printf("\n%.4f", sum);
    getch();
}
 (Note: Write separate user defined function to calculate power and factorial)

Post a Comment

0 Comments