blob: c8b2896b937d9e8ea100ca7d3bc07416b9a4f045 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#include <stdio.h>
#define STEPS 1000
double my_sin(double x)
{
int i;
double n = x;
double p = x;
for (i = 2; i <= STEPS * 2; i += 2) {
p *= -1 * x * x / (i * (i + 1));
n += p;
}
return n;
}
double my_cos(double x)
{
int i;
double n = 1;
double p = 1;
for (i = 2; i <= STEPS * 2; i += 2) {
p *= -1 * x * x / (i * (i - 1));
n += p;
}
return n;
}
int main(void)
{
double x;
printf("To calculate the sine and cosine of a given number\n\n");
printf("Enter a number: ");
scanf("%lf", &x);
printf("sin(%.14g) = %.14g\n", x, my_sin(x));
printf("cos(%.14g) = %.14g\n", x, my_cos(x));
return 0;
}
/*
Output:
Set 1:
To calculate the sine and cosine of a given number
Enter a number: 0
sin(0) = 0
cos(0) = 1
Set 2:
To calculate the sine and cosine of a given number
Enter a number: 1.5707963267948966
sin(1.5707963267949) = 1
cos(1.5707963267949) = 4.2647940513479e-17
Set 3:
To calculate the sine and cosine of a given number
Enter a number: 3.141592653579893
sin(3.1415926535799) = 9.9005553937036e-12
cos(3.1415926535799) = -1
Set 4:
To calculate the sine and cosine of a given number
Enter a number: -0.13720941543579
sin(-0.13720941543579) = -0.13677929342038
cos(-0.13720941543579) = 0.99060154698618
*/
|