This is a C 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 …).
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. */ #include <stdio.h> #include <math.h> #include <stdlib.h> void main() { int n, 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; n = 1; do { 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)); }
In this C program, we are reading the number of the terms in a series using ‘x’ variable. To convert degrees to radians the following formula is used
Sin(x) = x *(3.142/180.0).
Do while loop is used to compute the sum of the sine series. Compute the summation of the value of ‘n’ variable with 1 and multiply the value with 2 and again multiply with the value of ‘n’ variable.
Multiply the value of ‘x’ variable twice with the value of ‘term’ variable and take negation of the value then divide the value by ‘denominator’ variable. Compute the summation of the value of ‘sinx’ variable with the value of ‘term’ variable.
While condition is used to check the value of ‘accuracy’ variable is less than or equal to fabs() function value. If the condition is true, then execute the iteration of the loop. Print the value of sin(x) using printf statement.
$ 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.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Apply for Computer Science Internship
- Check Computer Science Books
- Check C Books
- Practice Computer Science MCQs
- Practice BCA MCQs