summary refs log tree commit diff stats
path: root/assignments/19-int-series.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/19-int-series.c
downloadcollege-b73983c3717642ca10e7cfe93d97609adc377da9.tar.gz
backup
Diffstat (limited to 'assignments/19-int-series.c')
-rw-r--r--assignments/19-int-series.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/assignments/19-int-series.c b/assignments/19-int-series.c
new file mode 100644
index 0000000..7403517
--- /dev/null
+++ b/assignments/19-int-series.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+
+int main(void)
+{
+	int i, sum, n, a;
+	printf("Calculate the sum of (-1)^(n+1) * i for i from 1 to given number.\n\n");
+	printf("Enter a number: ");
+	scanf("%d", &n);
+	a = 1;
+	sum = 0;
+	for (i = 1; i <= n; i++) {
+		sum += a * i;
+		a = -a;
+	}
+	printf("The sum is %d\n", sum);
+	return 0;
+}
+
+/*
+Output:
+Set 1:
+Calculate the sum of (-1)^(n+1) * i for i from 1 to given number.
+
+Enter a number: 1
+The sum is 1
+
+Set 2:
+Calculate the sum of (-1)^(n+1) * i for i from 1 to given number.
+
+Enter a number: 2
+The sum is -1
+
+Set 3:
+Calculate the sum of (-1)^(n+1) * i for i from 1 to given number.
+
+Enter a number: 10
+The sum is -5
+
+Set 4:
+Calculate the sum of (-1)^(n+1) * i for i from 1 to given number.
+
+Enter a number: 123
+The sum is 62
+
+*/
+