C Program to Calculate the Value of sin(x)

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)

C

How it works

  1. 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.
  2. 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.
  3. 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.
  4. The calculate_sin function returns the final value of sum, which represents the approximated value of sin(x) based on the Taylor series expansion.
  5. 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.
  6. The program calls the calculate_sin function, passing the values of x and n, and stores the result in the variable sin_x.
  7. 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.

Input/Output

C Program to Calculate the Value of sin(x)

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program finds the Greatest Common Divisor (GCD) of two given numbers. Problem Statement Write a C program that...
This C program calculates the roots of a quadratic equation of the form ax^2 + bx + c = 0....
This C program allows you to find the length of a linked list, which is the number of nodes present...