 # C Programming Code Examples

## C > Mathematics Code Examples

### Program to Calculate the Value of sin(x)

``` Program to Calculate the Value of sin(x) This C Program Calculates the Value of sin(x). It's a non-differentiable function. Start at zero, then goes up to 1, then back down to 0. But then, instead of going negative, it will just "reflect" about the x-axis. The derivative is 1 and then -1 for every x such that sin(x) = 0 (i.e. 0, 180, 360, 540, 720 ...). /* - C program to find the value of sin(x) using the series - up to the given accuracy (without using user defined function) - also print sin(x) using library function. */ #include <stdio.h> #include <math.h> #include <stdlib.h> void main() { int j, x1; float accuracy, term, denominator, x, sinx, sinval; printf("Enter the value of x (in degrees) \n"); scanf("%f", &x); x1 = x; /* Converting degrees to radians */ x = x * (3.142 / 180.0); sinval = sin(x); printf("Enter the accuracy for the result \n"); scanf("%f", &accuracy); term = x; sinx = term; j = 1; do { denominator = 2 * j * (2 * j + 1); term = -term * x * x / denominator; sinx = sinx + term; j = j + 1; } while (accuracy <= fabs(sinval - sinx)); printf("Sum of the sine series = %f \n", sinx); printf("Using Library function sin(%d) = %f\n", x1, sin(x)); } ``` 