diff options
author | smlckz <smlckz@college> | 2021-12-22 14:56:13 +0530 |
---|---|---|
committer | smlckz <smlckz@college> | 2021-12-22 14:56:13 +0530 |
commit | b73983c3717642ca10e7cfe93d97609adc377da9 (patch) | |
tree | a6e9fe4c27e3caa215f8aefa9265fb52f6de4375 /assignments/18-harmonic-series.c | |
download | college-b73983c3717642ca10e7cfe93d97609adc377da9.tar.gz |
backup
Diffstat (limited to 'assignments/18-harmonic-series.c')
-rw-r--r-- | assignments/18-harmonic-series.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/assignments/18-harmonic-series.c b/assignments/18-harmonic-series.c new file mode 100644 index 0000000..4fc62b1 --- /dev/null +++ b/assignments/18-harmonic-series.c @@ -0,0 +1,45 @@ +#include <stdio.h> + +int main(void) +{ + int i, n; + float sum; + printf("Calculate the sum of 1/i from i to a given number.\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + sum = 0; + for (i = 1; i <= n; i++) { + sum += 1/(float)i; + } + printf("The sum is %f\n", sum); + return 0; +} + +/* +Output: +Set 1: +Calculate the sum of 1/i from i to a given number. + +Enter a number: 1 +The sum is 1.000000 + +Set 2: +Calculate the sum of 1/i from i to a given number. + +Enter a number: 2 +The sum is 1.500000 + +Set 3: +Calculate the sum of 1/i from i to a given number. + +Enter a number: 10 +The sum is 2.928968 + +Set 4: +Calculate the sum of 1/i from i to a given number. + +Enter a number: 100 +The sum is 5.187378 + +*/ + |