Wednesday, 14 August 2013

C - Sum of 1/n numbers

C - Sum of 1/n numbers

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...
#include <stdio.h>
int main () {
int n, prefix;
float sum;
scanf("%d", &n);
do {
if (n%2==0) {
prefix=-1;
} else {
prefix=1;
}
sum+= premix/n;
n = n - 1;
} while (n > 0);
printf("%f", sum);
}

No comments:

Post a Comment