Need help with a program (absolute beginner)

Jan 20, 2008 at 1:40pm
Hej!
I'm studying C++ and could use all the help provided. This site is like a well in a desert :)
I have to use a repetition and create a program which sums 1 + 1/2 + 1/3+...1/n
I guess the for-loop is appropriate but it seems like I can't write a working one. Little help is needed, thanks :-)
Is there anyone I could bother with my quite dumb questions? :-P
Last edited on Jan 20, 2008 at 1:58pm
Jan 20, 2008 at 3:05pm
Maybe you forgot to cast to double 1/n
try with this code:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream.h>

void main() {
	double s = 0;
	long n = 1000000;

	for(long i = 1; i < n; i++)
		s = s + (double) 1/i;  // (double) cast the type of 1/i expression

	cout << "s = " << s << endl;
}
Topic archived. No new replies allowed.