Write a program to find sum of following series for a given value of ‘n’:
A) 1 + 1/2 - 1/3 + 1/4 - 1/5 + 1/6 +……+ 1/n
#include <stdio.h>
int main() {
int n, i;
double s;
printf("Enter a number: ");
scanf("%d", &n);
s = 1.0;
for(i=2; i<=n; i++) {
if (i%2 == 0) {
s += 1.0 / i;
}
else {
s -= 1.0 /i;
}
}
printf("The sum is equal %lf", s);
return 0;
}
Comments
Leave a comment