(adsbygoogle = window.adsbygoogle || []).push({}); 1. The problem statement, all variables and given/known data

Write a C++ program to determine and print the sum of the series (1/3 + 1/6 + 1/9 + ... + 1/(3*n)) for a given value of n. In case you don't see the patter, the series can also be written as 1/(3*1) + 1/(3*2) +1/(3*3)+ ... + 1/(3*n). The value of n should be given interactively through the terminal. (You won't get credit if you do not use loops)

2. Relevant equations

[itex]Series = \frac{1}{3} \sum^{n}_{k=1} \frac{1}{k}[/itex]

3. The attempt at a solution

//The following code gives me a final answer of 0. I can't understand why my FOR loop doesn't work. Please help!

#include <iostream>

#include <cmath>

using namespace std;

int main( void )

{

int iN = 0;

int iK = 1;

cout << "Input a positive integer value for n for the summation of 1/(3k), from k=1 to k=n." << endl;

cin >> iN;

if(iN <= 0)

{

cout << "Invalid value for n. Exiting..." << endl;

return 0;

}

double dSum=0.0;

for(iK=1; iK <= iN; iK++)

{

dSum += double(1/iK);

}

cout << "The value of that summation is " << (1/3)*dSum << "." << endl;

return 1;

}

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# C++: Simple summation program won't work correctly

**Physics Forums | Science Articles, Homework Help, Discussion**