TASK-
Write a C program for Taylor series of sin(x).
STEPS TO BE TAKEN:
1)- 1st we have to take number of terms and the value of x
2)- As the sign of each term is alternate we will use a separate variable for it
3)- An algorithm is required for factorial
4)- An variable is required to sum up all the terms;
5)- Other points are discussed in the program below
For better understanding of Point 3 click here ---> FACTORIAL
CODE:
#include<stdio.h>
#include<math.h>
int main(){
int n,x; //to apply the formula of taylor series of sine we need number of terms and vaule of x
printf("Enter the number of terms: ");
scanf("%d",&n); //getting number of terms in n
printf("\nEnter the value of x: ");
scanf("%d",&x);//Now its time get value of x;
int sign=1; // this is the variable which we will use for sign change
float sum=0;
int fact=1; // factorail calculation algorithm require this variable
int i,j,u=1,power;
printf("\n\n");
for (i=1;i<=n;i++)
{
fact=1;
//As in each term we need to find factorail newly so it is required to put fact =1 each time
//Algo for factorial
for (j=1;j<=u;j++)
{
fact=fact*j;
}
fact=fact*sign;
power=pow(x,u);
sum=sum+(float (power)/fact);
sign=sign*(-1);
//printing may effect so the following condition are served
if (i!=1 && fact>0)
printf("+ (%d^%d)/%d! ",x,u,u);
else if (i!=1 && fact<0)
printf("- (%d^%d)/%d! ",x,u,u);
else
printf("%d ",x);
u=u+2; // value of each term is changing with odd order
}
printf("\nTHE SUM OF SERIES IS %0.5f",sum);
getchar();
return 0;
}
Expected Output: