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".
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
.
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 the code inside the for loop
is executed and updates 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.
For more about loops in C refer here
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) .
#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() */
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