Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #define EPSILON 0.0001
- double factorial(double x)
- {
- if (x <= 1) {
- return 1;
- } else {
- return x*factorial(x-1);
- }
- }
- int main()
- {
- int x;
- printf("Enter x: ");
- scanf("%i", &x);
- double answer = 0;
- int sign = 1; // For alternating signs
- for (int n = 0 ;; n += 2) {
- double new_term = sign * (pow(x, n) / factorial(n));
- answer += new_term;
- sign *= -1;
- if (abs(new_term) < EPSILON)
- break;
- }
- printf("Answer: %f\n", answer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement