C++ program

2008-01-14 7:23 am
I need a program that takes n as a parameter and returns the sum of the first n elements in the sequece:
1/2+2/3+3/4+....+(n/n+1)
where n is equal to 8.
The function needed to be named as 'sum' and it should be a recursive function.

pls be as simple as possible and not using iostream.h

thx a lot!

回答 (1)

2008-01-14 9:05 am
✔ 最佳答案
The answer is 6.17103..., or precisely 15551/2520 if you want more precision for verifcation purposes.
The recursive method itself takes the header with one integer parameter and two statements, so it should be simple as you requested. The method sum is reproduced here, but for clarity you can view the function with the complete test program by clicking at this link.

double sum(int n)
{
if(n==1)return (double)1/2;
return sum(n-1)+(double)n/(n+1);
}


收錄日期: 2021-04-13 14:57:48
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080113000051KK05016

檢視 Wayback Machine 備份