C Program to find the value of Sinx up to the given accuracy

For a better understanding of this C program example, we always recommend you to learn the basic topics of C programming listed below:

What is a sinx trigonometric function?

Here in this C program, we will find out the value of sine x up to the given accuracy. Sine is the trigonometric function of an angle of a right triangle. For an angle, sine value is defined as the ratio of length and side opposite that angle to the hypotenuse. [ means the longest side of the triangle].
Normally all trigonometric functions are defined as the ratio of sides of the triangle, which have an angle too. The sine values for the defined angles are sine 0 = 0, sine 90 = 1, sine 30 = .5 etc.
Here this c program, we find out the value of sine x up to the given accuracy. For that, firstly, we are converting the given degrees to radians. We are using a do-while loop and use the steps given below.

         den = 2*n*(2*n+1);
         term = -term * x * x / den;
         sinx = sinx + term;
         n = n + 1;

How sinx calculation is implemented in C

In this C program, after importing the header files, we are accepting the value of x in degrees. We save that value in a variable, and now to apply the formula, we need to convert the degree to radians using the formula 'pi / 180'. We get the needed accuracy from the user, and we save the value of the angle in radian in a variable 'x,' and we copy the 'x' to another variable and assign that to the sin(x). Now open a do-while loop to check calculate value using den =2*n*(2*n+1), then using 'den' we calculate the term = -term * x * x / den; .And sinx - sinx = sinx + term; we do the loop until the condition acc <= fabs(sinval - sinx) will be true. Finally printing the value of sinx as a result.

ALGORITHM

STEP 1: Import the Header files into the C program to use the built-in functions.

STEP 2: Accept the value of the angle in Degree using printf and scanf and save that in the variable.

STEP 3: Initialize and Define the variable for the C program.

STEP 4: Save the value of the angle in Degree into a variable and convert the Angle to Radians using 'pi / 180'.

STEP 5: Assign the value of sin(x) to a variable using sinval

STEP 6: Accept the accuracy value from the user using printf and scanf and save that to a variable. 

STEP 7: Save the value of term =x and sinx = term and n = 1. 

STEP 8: Open a 'do while' loop condition till the condition meets (acc <= fabs(sinval - sinx)).

STEP 9: Calculate the value of 'den' by using the formula den = 2*n*(2*n+1).

STEP 10: Calculate the Term Value using the formula -term * x * x / den. Using that, calculate the value of sinx by sinx + term and increment the n by 1.

STEP  11: Print the result of the Sine series using the printf and using the C programming language.

C Source Code

                                          #include <stdio.h>
#include <math.h>
#include <stdlib.h>

void main()
     { 
         int n, x1;
         float  acc, term, den, x, sinx=0, 
         sinval;                                                /* declares acc, term, den, sinx, sinval as float */
         printf("Enter the value of x (in degrees)\n");            /* gets the value of x in degrees */
         scanf("%f",&x);
         x1 = x;
         x = x*(3.142/180.0);                                                 /* Converting degrees to radians*/           
         sinval = sin(x);
         printf("Enter the accuracy for the result\n");                 /* receives value of accuracy */
         scanf("%f", & acc);
         term = x;
         sinx = term;
         n = 1;
         do
            {
                 den = 2*n*(2*n+1);
                 term = -term * x * x / den;                      /* enters do while loop and use the formula to find den */
                  sinx = sinx + term;                                                    /* find term using -term*x*x/den */
                  n = n + 1;
            }
         while(acc <= fabs(sinval - sinx));                                     /* while condition checking */
         printf("Sum of the sine series         = %f\n", sinx);
         printf("Using Library function sin(%d) = %f\n", x1,sin(x));  /* prints the value of sinx */
    }                                                                
                                      

OUTPUT

Enter the value of x (in degrees)
30
Enter the accuracy for the result
0.000001
Sum of the sine series         = 0.500059
Using Library function sin(30) = 0.500059

RUN 2

Enter the value of x (in degrees)
45
Enter the accuracy for the result
0.0001
Sum of the sine series         = 0.707215
Using Library function sin(45) = 0.707179