Code

dipen.pandit

Dipen Pandit

• April 21, 2022, 2:58 p.m.

Program to calculate the value of cos(x) by expansion

hint: cos(x) = 1 ? x^2/ 2! + x^4/4 !

C

#include
#include
int factorial(int i)
{
    int j;
    long fact=1;
    if(j=0)
    {
        return 1;
    }
    else
    {
        for(j=1;j<=i;j++)
        {
            fact=fact*j;
        }
        return fact;
    }
}
int main()
{
    int i,n,sign=-1;
    float x,x1,sum=0;
    printf("Enter the value of x (in degree) \n");
    scanf("%f",&x);
    printf("Enter the number of terms \n");
    scanf("%d",&n);
    x1=x;
    x=x*(3.1415/180);
    for(i=0;i<=n;i+=2)
    {
        sign= -1*sign;
        sum=sum + sign*pow(x,i)/factorial(i);
    }
    printf("cos(%.0f)=%.2f",x1,sum);
    return 0;
}
Output:
Program to calculate the value of cos(x) by expansion | Output of C Language Code

Add comment:

Total 0 comments