Skip to content

Instantly share code, notes, and snippets.

@jagdish4501
Created May 10, 2021 13:05
Show Gist options
  • Save jagdish4501/ef6a4e205af8a9caf43e79b68619955e to your computer and use it in GitHub Desktop.
Save jagdish4501/ef6a4e205af8a9caf43e79b68619955e to your computer and use it in GitHub Desktop.
Let us c Q.Write a C function to evaluate the series sin(x)=x-(x^3 / 3! ) + (x^5 / 5! ) - (x^7 / 7! ) + ................... to five significant digits.
#include <stdio.h>
#include <math.h>
long fact(int);
int main()
{
int x, i = 1;
float fn = 1, sum = 0;
printf("Enter value of x=");
scanf("%d", &x);
for (int k = 0; fn >= 0.000000000001; k++)
{
fn = pow(x, i) / (float)fact(i);
if (k % 2 == 0)
{
sum = sum + fn;
}
else
sum = sum - fn;
i = i + 2;
}
printf("\nsin(%d)=%0.9f", x, sum);
}
long fact(int x)
{
if (x == 1)
{
return 1;
}
else
return x * fact(x - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment