122. C Program to Compute the 1-x2/2!+x4/4!– x6/6! +……………….+ xn/n! cosine series.
How does this program work?
- This program is used to compute the sum of series for the given cosine order.
- Here we are going to find the series for 1-x2/2! + x4/4! – x6/6! + ………………………….+ xn/n!upto nth term.
- where The nth term is the user input.
- Computing the given series is easy by Using the simple for loop condition and by using the cosine equation.
Here is the code
#include <stdio.h>
int main(void)
{
int i, n;
float x, sum=1, t=1;
printf( "Enter the value for x : \n");
scanf("%f",&x);
printf( "Enter the value for n : \n");
scanf("%d",&n);
x=x*3.14159/180;
// Loop to calculate the value of Cosine
for(i=1;i<=n;i++)
{
t=t*(-1)*x*x/(2*i*(2*i-1));
sum=sum+t;
}
printf(" The value of Cos(%f) is : %.4f", x, sum);
return 0;
}