This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical series that represents a function as an infinite sum of terms. In this case, we use the Taylor series for sine to approximate the sine of a given angle (in radians).

The sine function is a fundamental trigonometric function that relates the angle of a right triangle to the ratio of the length of the side opposite the angle to the length of the hypotenuse. In mathematics, the sine function is typically defined using a series expansion known as the Taylor series.

## Program Statement

Write a C program that calculates the value of sin(x) using the Taylor series approximation. The program should prompt the user to enter the value of the angle `x`

in radians and the number of terms `n`

to include in the series. The program should then calculate the approximated value of sin(x) using the Taylor series expansion and print the result to the console.

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

#include <stdio.h> #include <math.h> double calculateSin(double x, int terms) { double radians = x * (M_PI / 180.0); double result = radians; // First term of the Taylor series for (int i = 1; i <= terms; i++) { double numerator = pow(-1, i) * pow(radians, 2 * i + 1); double denominator = 1; for (int j = 1; j <= 2 * i + 1; j++) { denominator *= j; } result += numerator / denominator; } return result; } int main() { double angle; int numTerms; printf("Enter the angle in degrees: "); scanf("%lf", &angle); printf("Enter the number of terms to approximate: "); scanf("%d", &numTerms); double sinValue = calculateSin(angle, numTerms); printf("The sin(%lf) is approximately %lf\n", angle, sinValue); return 0; }

## How it works

- The program starts by including the necessary header files:
`stdio.h`

for input/output operations and`math.h`

for mathematical functions like`pow`

and`tgamma`

. - The
`calculate_sin`

function is defined, which takes two parameters:`x`

(the angle in radians) and`n`

(the number of terms to include in the series). This function calculates the value of sin(x) using the Taylor series expansion. It initializes a variable`sum`

to store the cumulative sum of the terms. - Inside the
`calculate_sin`

function, a`for`

loop is used to iterate from 0 to`n-1`

. Each iteration calculates a term of the Taylor series and adds it to the`sum`

. The exponent of the term is calculated as`2 * i + 1`

, and the term itself is calculated using the formula`pow(-1, i) * pow(x, exponent) / tgamma(exponent + 1)`

. The`pow`

function raises`-1`

to the power of`i`

and`x`

to the power of the exponent. The`tgamma`

function calculates the factorial of the exponent (`exponent + 1`

) to be used as the denominator of the term. - The
`calculate_sin`

function returns the final value of`sum`

, which represents the approximated value of sin(x) based on the Taylor series expansion. - In the
`main`

function, the program prompts the user to enter the value of`x`

(the angle in radians) and reads it using`scanf`

. Similarly, it prompts the user to enter the number of terms`n`

and reads it as well. - The program calls the
`calculate_sin`

function, passing the values of`x`

and`n`

, and stores the result in the variable`sin_x`

. - Finally, the program uses
`printf`

to display the result to the console, showing the value of`x`

, the calculated value of sin(x), and a message indicating the result is an approximation.

That’s a high-level overview of how the C program works. It takes the user inputs, calculates the approximated value of sin(x) using the Taylor series expansion, and displays the result.