Computer Scientists are Pretty Pessimistic

Friday 4 September 2015

Write a function that returns the sum of the following series where x and n are passed to the function as argument. (1 + x2/2! + x4/4! + x6/6! + …….xn/n!)

#include <stdio.h>
double sum (int x, int n);
int main ()
{
    int x,n;
    double res;
    scanf("%d %d",&x,&n);
    res=sum(x,n);
    printf("%.3lf",res);
    return 0;
}
double sum (int x, int n)
{
    int i,j;
    double s=1,f,p;
    for(i=2;i<=n;i+=2)
    {
        f=1;
        p=1;
        for(j=0;j<i;j++)
        {
            p=p*2;
        }
        for(j=1;j<=i;j++)
        {
            f=f*j;
        }
        s=s+(p/f);
    }
    return s;
}

No comments:

Post a Comment