c program

2007-01-22 10:39 am
2) Calculate the value of π from the infinite series.


Print a table that shows the value of π approximated by one tern of this series, by two terms, by three terms, etc. as follow:


pi(1)=2.666667
pi(10)=3.232316
pi(100)=3.151493
pi(1000)=3.142592
pi(10000)=3.141693
pi(100000)=3.141603
pi(1000000)=3.141594
pi(10000000)=3.141593
pi(100000000)=3.141593
pi(1000000000)=3.141593

以上個題點做~~?(use c program)
好緊急,,因為後日考試
唔該~~

回答 (1)

2007-01-22 7:49 pm
✔ 最佳答案
以下的algorithm用上以下的infinite series:
pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...

1. accept user input and store to a variable noOfTerms
2. if noOfTerms is greater than zero
2.1 initialize loop_count and pi_over_4 to zero
2.2 for loop_count equals 0 to noOfTerms (inclusively)
2.2.1 if loop_count is even
2.2.1.1 pi_over_4 increments by 1 / (2 * loop_count + 1)
2.2.2 if llop_count is odd
2.2.2.1 pi_over_4 decrements by 1 / (2 * loop_count + 1)
2.3 output pi as pi_over_4 times 4
3. if noOfTerms is smaller than or equals to zero
3.1 print input error message


收錄日期: 2021-04-23 20:06:36
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070122000051KK00325

檢視 Wayback Machine 備份