Ilteration code C++

Hello. I need a simple code which would calculate me this (need to use one of theses functions: while, for, do-while):

(2*n)/(1+2+3+...+n)

I have no idea how to do it.

Any ideas?
1
2
3
4
5
6
int n;
std::cin >> n;
double sum = 0;
for (int i = 1; i <= n; ++i)
    sum += i;
double result = 2 * n / sum;
Topic archived. No new replies allowed.