summary refs log tree commit diff stats
path: root/assignments/21-sin-cos.c
diff options
context:
space:
mode:
authorsmlckz <smlckz@college>2021-12-22 14:56:13 +0530
committersmlckz <smlckz@college>2021-12-22 14:56:13 +0530
commitb73983c3717642ca10e7cfe93d97609adc377da9 (patch)
treea6e9fe4c27e3caa215f8aefa9265fb52f6de4375 /assignments/21-sin-cos.c
downloadcollege-b73983c3717642ca10e7cfe93d97609adc377da9.tar.gz
backup
Diffstat (limited to 'assignments/21-sin-cos.c')
-rw-r--r--assignments/21-sin-cos.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/assignments/21-sin-cos.c b/assignments/21-sin-cos.c
new file mode 100644
index 0000000..c8b2896
--- /dev/null
+++ b/assignments/21-sin-cos.c
@@ -0,0 +1,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
+
+*/
+