TASK-
Write a program in C to find the sum of the Taylor series of cos(x)
Taylor series of Cos(x):
1 - x²/2! + x⁴/4!...….
CODE:
#include<stdio.h>
#include<math.h> // To use power function
int main(){
int y=2,n,i,sign=-1,fact=1,j;
float x,sum=0,eq;
//Asking for value of x and number of terms n
printf("Enter the value of x and number of terms n ");
printf("\nX: ");
scanf("%f",&x); // x from here
printf("\nN: ");
scanf("%d",&n); //number of terms(n) here
printf("\n");
printf("1");
for (i=2;i<=n;i++)
{
fact=1;
for (j=1;j<=y;j++) /*Calculating the factorial (for better understanding about factorial program click here 👉👉 Factorial and if you want factorial by recursion click here 👉👉 Factorial by recursion)*/
{
fact=fact*j;
}
eq=((pow(x,y))/fact)*sign;
sum=sum+eq;
if (sign<0)
printf("-(%0.2f ^ %d /%d!)",x,y,y);
else
printf("+(%0.2f ^ %d /%d!)",x*sign,y,y);
sign=sign*-1; //sign is changing after each term
y+=2; //because the series is going in even order x^2/2! the x^4/4!
}
sum=sum+1;
printf("\nSum of series = %f ",sum);
getchar();
return 0;
}
Expected Output:
This is great thing 👍👍
ReplyDelete