In this tutorial, we will write a C Program to find Cosine series. Let us first by understanding what is cosine series.
Cosine Series:
It is a series used to find the value of cos(x), where x is the angle in degree which is converted to Radian.
Formula:
For Cos(x) series:
Cos(x) = 1 – (x*2 / 2!) + (x*4 / 4!) – (x*6 / 6!)……
Let us understand through a C program to calculate the value of cos(x).
Example
Input-:
x = 4, n = 3
Output-:
0.997563
Input-:
x = 8, n = 2
Output-:
0.990266
C Program to find Cosine series
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <stdio.h> const double PI = 3.142; int main() { float x; int n; float result = 1; float s = 1, fact = 1, p = 1; printf("Enter x value: "); scanf("%f", &x); printf("Enter n value: "); scanf("%d", &n); //Converting degrees to radians x = x *(PI / 180.0); for (int i = 1; i < n; i++) { s = s *-1; fact = fact *(2 *i - 1) *(2 *i); p = p *x * x; result = result + s *p / fact; } printf("Result: %lf\n", result); return 0; } |
Output:
Enter x value: 10
Enter n value: 3
Result: 0.984804