Hello could you take a quick glance at my code and point out the mistake(s). I'm trying to calculate sum of n numbers going like this: 1- 1/2 + 1/3 - 1/4 ... etc...
With the following code, I get 1.00000 each time, but it should be between 0 and 1, for example for 3 it should be 1 - 1/2 + 1/3 = 0,83333.
#include <stdio.h>
int main () {
int n, prefix;
float sum;
scanf("%d", &n);
do {
if (n%2==0) {
prefix=-1;
} else {
prefix=1;
}
sum+= prefix/n;
n = n - 1;
} while (n > 0);
printf("%f", sum);
}