Finding value of cosx using recursive function


#include
float num(float, float);
float fact(float);

void main()
{
    float x, term=0, sum=0, n, s, i;
    printf("Enter the number of terms: ");
    scanf("%f", &n);
    printf("Enter x in degrees: ");
    scanf("%f", &x);
    x = x*3.1415/180;
    s=1.0;
    for(i=0.0; i‹n; i++)
    {
        term = num(x, (2*i))/fact(2*i);
        if(s==1)
            sum = sum+term;
        else
            sum = sum-term;
        s*=(-1);
    }
    printf("Value is %f", sum);
}

float num(float x, float n)
{
    if(n==0)
        return 1;
    else
        return x*num(x, n-1);
}

float fact(float n)
{
    if(n==0)
        return 1;
    else
        return n*fact(n-1);
}

Leave a Reply

Your email address will not be published. Required fields are marked *