reset password
Author Message
smirand8
Posts: 8
Posted 18:14 Oct 16, 2012 |

Write a program to read the value of n and calculate the following series.

1 + 1/3 + 1/5 + 1/7 + ... + 1/(2n-1)

 

For some reason I can't get the output right , if I put input as 1 I get output 1 but for any other number I put as input I get 0 as output.

kevinTnt
Posts: 28
Posted 18:36 Oct 16, 2012 |

01101000 01101001

Tanko24
Posts: 8
Posted 18:51 Oct 16, 2012 |

What's ur code calculations look like ?

smirand8
Posts: 8
Posted 19:02 Oct 16, 2012 |

int n = input.nextInt();

int total = 0;

for (int i=0; i<n; i++)

{total = total+1/(2*n-1);}

kknaur
Posts: 540
Posted 20:01 Oct 16, 2012 |

You need to figure out if the series starts from n = 0 or n = 1

kevinTnt
Posts: 28
Posted 20:03 Oct 16, 2012 |

starts from zero .

kevinTnt
Posts: 28
Posted 20:04 Oct 16, 2012 |

wait i  meant 1 .

jpascua
Posts: 197
Posted 21:05 Oct 16, 2012 |

Isn't it supposed to be {total = total+1/(2*i-1);} not "n"?

jpascua
Posts: 197
Posted 21:07 Oct 16, 2012 |

You would be multiplying with the input if so. And the input never increases nor decreases. It stays constant. What you multiply it is by "i" which is the changing number (that increases after every loop).

jkiyomu
Posts: 27
Posted 23:16 Oct 16, 2012 |

You can use (total += 1/(2n-1)) n cannot equal 0

Wesleyla2011
Posts: 4
Posted 00:46 Oct 17, 2012 |

Total += 1.0/(2*i - 1) with i start from 1

Last edited by Wesleyla2011 at 23:52 Oct 17, 2012.