# C Program to find sum of Cos(x) series

April 2, 2022, Learn eTutorial
2692

## What is the cos x series?

The trigonometric capacity is utilized to relate the edges of a triangle to its sides. Cosine, which we call cosx, is the proportion of adjacent side length to hypotenuse length. "cosA = Adjacent/ Hypotenuse".

The cos X Series contains even powers and factorials. Generally the formula of Cosine series is

cosx = 1 - (x^2/2!) + (x^4/4!) - (x^6/6!) + ..........

In this C program, we accept the input from the user in degrees and convert it to radians to make calculations. Then we open a `for loop` and we use the formula cosx = cosx + (pow (x,i) /fact) *sign ; sign = sign *(-1); to calculate the sum of the cos x. For using the function pow(), we have to include the 'math.h' header file into the program. It is a simple program to understand. Here we use `for loop`.

### ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the integer variables n, x1, i, j.

STEP 3: Declare the variables x, sign, cosx, fact as type `float`.

STEP 4: Read the number of terms in the Series to n.

STEP 5: Read the value of angle into the variable x.

STEP 6: Assign x1=x.

STEP 7: x=x*(3.142/180.0)

STEP 8: Assign cosx=1 and sign=-1 and i=2.

STEP 9: Use a `for loop` with the condition 'i<=n' true do steps 10 to 13 by incrementing i by 1.

STEP 10: set fact=1.

STEP 11: using another `for loop` with condition j<=i true  do fact=fact*j by incrementing j by 1.

STEP 12: cosx=cosx+(pow(x,i)/fact)*sign.

STEP 13: sign=sign*(-1).

STEP 14: Display the sum of the cosine series is cosx.

STEP 15: Display the value of cos(x1) using library function cos(x).

This cosine series program uses the loops concept, For more about loops in C refer here

## C Source Code

```                                          ```#include<stdio.h>
#include<math.h>

void main() {
int n, x1, i, j;
float x, sign, cosx, fact;
printf("Enter the number of the terms in a series\n");
scanf("%d", & n);
printf("Enter the value of x(in degrees)\n");
scanf("%f", & x);
x1 = x;
x = x * (3.142 / 180.0); /* Degrees to radians*/ /* converting the degrees into radians */
cosx = 1;
sign = -1;
for (i = 2; i <= n; i = i + 2) {
fact = 1;
for (j = 1; j <= i; j++) {
fact = fact * j;
}
cosx = cosx + (pow(x, i) / fact) * sign; /* calculating the cosx x sum */
sign = sign * (-1);
}
printf("Sum of the cosine series= %7.2f\n", cosx);
printf("The value of cos(%d) using library method = %f\n", x1, cos(x));

} /*End of main() */```
```

## OUTPUT

```Enter the number of the terms in a series
5

Enter the value of x(in degrees)
60
Sum of the cosine series                    =    0.50
The value of cos(60) using library method = 0.499882```

VIEW ALL
VIEW ALL