Computer Scientists are Pretty Pessimistic

Wednesday 23 September 2015

WAP to find out the sum of the following series.


                                                                        1 + x2/2! + x4/4! + x6/6! + …….xn/n!


CODE : :

#include <stdio.h>
#include <math.h>
long long fact(int n);
int main ()
{
    long long i,x,n;
    double sum=1;
    printf("Enter x and n: ");
    scanf("%lld %lld",&x,&n);
    for(i=2;i<=n;i=i+2)
    {
        sum=sum+pow(x,i)/(double)fact(i);
    }
    printf("Sum is : %.3lf\n",sum);
}
long long fact(int n)
{
    if(n==1)
        return 1;
    return n*fact(n-1);
}

No comments:

Post a Comment