Help Please.

Feb 20, 2013 at 10:37pm
Write a program using for loop construct to compute the sum of:
1/2 + 1/3 + 1/4 + … + 1/10
Feb 20, 2013 at 10:41pm
1
2
3
4
float v = 0.0;
for(int i = 2; i <= 10; i++) {
	v += 1/i;
}
Feb 20, 2013 at 11:03pm
i tried this but it wouldnt work

#include "stdafx.h"
#include <iostream>
using namespace std;

int main ()
{
double v = 0;
for(int i = 2; i <= 10; i++)
{
v += 1/i;
}
cout << v << endl;
system("Pause");
return 0;
}
Feb 20, 2013 at 11:09pm
this works on my end:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main ()
{
float v = 0;
for(int i = 2; i <= 10; i++)
{
	v += (float)1/i;
}
cout << v << endl;
return 0;
}


I had forgotten the cast to float in my last post.

EDIT: And please use code-tags when posting code:)
Last edited on Feb 20, 2013 at 11:10pm
Feb 20, 2013 at 11:13pm
ty<3
Topic archived. No new replies allowed.