Help Please.

Write a program using for loop construct to compute the sum of:
1/2 + 1/3 + 1/4 + … + 1/10
1
2
3
4
float v = 0.0;
for(int i = 2; i <= 10; i++) {
	v += 1/i;
}
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;
}
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
ty<3
Topic archived. No new replies allowed.