How do you make a sum? 1/(a+i) i++

Hello i was wondering

how do you make a sum?
here is the program. I try to make a sum of 1/(a+i)... i goes from 0 to 1000. and the program is supposed to plus all those terms together and print out the result.

#include iostream
using namespace std;
main(){
int a=1000000;
float b=0;
for (int i=0;i<=1000;i++)
b+=1/(a+i);
cout<<b;
return 0;
}

Thanks
Last edited on
It is better to define b as double.

double b=0;
for (int i=0;i<=1000;i++)
b += 1.0 / (a+i);
Topic archived. No new replies allowed.