1/25 + 2/23 + 3/21 + … + 12/3 + 13/1 =?

The source code of this by simple loop

1/25 + 2/23 + 3/21 + … + 12/3 + 13/1

?

what is the errors of this code:
 ``1234567891011121314151617181920`` ``````#include "stdafx.h" #include using namespace std; int main() { int i,j; long double sum = i/j; cout << " Press Enter to calculate 1/25 + 2/23 + 3/21 + ... + 12/3 + 1/13 "; for ( i=1 , i<=13 ,i++;) { for (j=25, j>=1,j--;) cout << " 1/25 + 2/23 + 3/21 + ... + 12/3 + 1/13 = " << sum; } cin.ignore(); cin.get(); }``````

What's right with it?
I suggest you review the syntax of the for-loop in the tutorial:
http://www.cplusplus.com/doc/tutorial/control/

Various other issues. The cout statement is inside one of the loops. It should appear after the end of the loop. Two loops are not required. Just one will suffice. You need to initialise the total (sum) to zero, then add each term of the series to it inside the loop.

The division i/j at line 8 is not needed at that point (but it could make a great deal of sense inside the loop). Because neither i nor j have been initialised on line 7, then on line 8 the result will be garbage at best, and at worst might involve division by zero which would cause a program exception (crash).

When you do the division, if both variables are type int, then integer division will be used. In this case you need floating point division.
Last edited on
Special thanks for guidance,it has been solved now.

 ``1234567891011121314151617181920`` ``````#include "stdafx.h" #include using namespace std; int main() { long double i,j; long double sum=0; cout << " Press Enter to calculate 1/25 + 2/23 + 3/21 + ... + 12/3 + 1/13 : " <=1, i<=13;j-=2,i++){ sum = sum + (i/j); cout << "\n " << i/j ; } cout << "\n\n\n\n SUM of them is : " <
Topic archived. No new replies allowed.