0
#include <stdio.h>
#include <string.h>

int main()
{
float s=0;
int i, n;

printf("Quantity of terms:");
scanf("%f", &n);

for(i==0; i<=n-1; i++);
{
    s=s+1/(i+1);
}

printf("Sum: %f", s);
return 0;
}

Hi. I'd like to sum up the series $1+1/2+...+1/n$, but I always get the sum zero... Could someone help me?

Thank you!

Quantity of terms:2
Sum: 0.000000
Process returned 0 (0x0)   execution time : 7.953 s
Press any key to continue.
Quiet_waters
  • 209
  • 1
  • 6

1 Answers1

8

I see several problems with your code:

  • type error in scanf (n is an integer, not a float)
  • i=0 and not i==0 in the for loop
  • spurrious ; at the end of the for line
  • 1/(i+1) is always evaluated to 0 because 1 and i+1 are integers

Here's one that works on my end:

#include <stdio.h>
#include <string.h>

int main() { float s=0.; int i, n;

printf("Quantity of terms:"); scanf("%d", &n);

for(i=0; i<=n-1; i++) { s=s+1./(i+1); }

printf("Sum: %f", s); return 0; }

Miyase
  • 196
  • 2
  • 5