C Program to Calculate the Value of sin(x)

This is a C Program to calculate the value of sin(x).

Problem Description

This C Program Calculates the Value of sin(x).

Problem Solution

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 …).

Program/Source Code

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));
}
Program Explanation

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).

advertisement
advertisement

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.

Runtime Test Cases
 
$ 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

advertisement
If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.