Code
Program to calculate the value of cos(x) by expansion
hint: cos(x) = 1 ? x^2/ 2! + x^4/4 !
Rate
#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;
}
Add comment:
Total 0 comments