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 …).
Here is source code of the C program to Calculate the Value of sin(x). The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* 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.
int n, x1;
float accuracy, term, denominator, x, sinx, sinval;
printf("Enter the value of x (in degrees) \n");
x1 = x;
/* Converting degrees to radians */
x = x * (3.142 / 180.0);
sinval = sin(x);
printf("Enter the accuracy for the result \n");
term = x;
sinx = term;
n = 1;
denominator = 2 * n * (2 * n + 1);
term = -term * x * x / denominator;
sinx = sinx + term;
n = n + 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));
$ cc pgm14.c -lm $ a.out Enter the value of x (in degrees) 60 Enter the accuracy for the result 0.86602540378443864676372317075294 Sum of the sine series = 0.855862 Using Library function sin(60) = 0.866093 $ a.out Enter the value of x (in degrees) 45 Enter the accuracy for the result 0.70710678118654752440084436210485 Sum of the sine series = 0.704723 Using Library function sin(45) = 0.707179
Sanfoundry Global Education & Learning Series – 1000 C Programs.