C Program to find sum of Cos(x) series

In this C program, we have to find the Sum of 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".

What is the cos x series?

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 in this program. It is a simple program to understand. Here we use for loop.

What is the syntax of for loop?

The syntax of a for loop is given by


for (initializationStatement; testExpression; updateStatement)

     {
          // codes
     }

Here the initialization statement is executed only once. Initially, the test expression is evaluated. If the test expression is False, then for loop is terminated. But if the test expression is True, then code inside the for loop is executed and update the expression. This process continues until the test expression is False. This type of loop is commonly used when the number of iterations is already known.

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 'x' 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: By using for loop with the condition 'i<=n' do step 10.

STEP 10: fact=1.

STEP 11: Assign j=1.

STEP 12: By using for loop with condition j<=i  do step 13.

STEP 13: fact=fact*j.

STEP 14: Increment j by 1, and do step 12.

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

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

STEP 17: Increment 'i' by 1 and do step 9.

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

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

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"); /* enter the series */
    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