C program to compute sin(x) using Taylor’s series: In this program we will use taylor series to compute sin(x), code for the problem is given below.
Code for C program to compute sin(x) using Taylor’s series
#include <stdio.h>
#include <math.h>
int main()
{
float fact(float);
float sum = 0, x, range;
int i, j;
printf("Enter the X value:");
scanf("%f", &x);
x = x * 3.14 / 180.0;
printf("Enter the range value:");
scanf("%f", &range);
for (i = 1, j = 1; i <= range; i++, j = j + 2)
{
if (i % 2 != 0)
sum = sum + pow(x, j) / fact(j);
else
sum = sum - pow(x, j) / fact(j);
}
printf("%f", sum);
}
float fact(float x)
{
int f = 1, i;
for (i = 1; i <= x; i++)
f = f * i;
return f;
}
Output
Enter the X value:30
Enter the range value:4
0.499770
Read: C program to check whether the given number is prime or not