Created
June 1, 2011 08:07
-
-
Save nsmgr8/1001958 to your computer and use it in GitHub Desktop.
sine taylor series for projanmo forum query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
long fact(long p) { | |
long i, mult = 1; | |
for(i=1; i<=p; i++) | |
mult *= i; | |
return mult; | |
} | |
float sinex(float x, int n) { | |
int m, i, j = 1; | |
float sum = 0; | |
for(i=0; i<n; i++) { | |
m = 2 * i + 1; | |
sum += j * pow(x, m) / fact(m); | |
j = -j; | |
} | |
return sum; | |
} | |
int main() { | |
int i, n; | |
float x; | |
while(1) { | |
printf("Number of terms (enter 0 to quit): "); | |
scanf("%d", &n); | |
if(n == 0) | |
break; | |
printf("Enter a value for x: "); | |
scanf("%f", &x); | |
printf("\nValue of sin(%f) is %f\n", x, sin(x)); | |
printf("The series gave me %f\n\n", sinex(x, n)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment