原文:http://www.cnblogs.com/imaker/p/6128049.html
所屬年份:2010.9;2012.3
編寫函數fun,其功能是:根據以下公式求π的值(要求精度0.0005,即某項小於0.0005時停止迭代)。
程序運行後,若輸入精度0.0005,則程序應輸出為3.14…。
注意:部分源程序在文件PROG1.C中。
請勿改動主函數main和其它函數中的任何內容,僅在函數fun的花括號中填入你編寫的若干語句。
代碼如下:
#include <stdio.h>
#include <conio.h>
#include <math.h>
double fun (double eps)
{
double s =1.0,sl=1.0;
int n=1;
while(sl>=eps)
{
sl=sl*n/(2*n+1);
s=s+sl;
n++;
}
return 2*s;
}
void main()
{
double x;
printf("Input eps:");
scanf("%lf" ,&x);
printf("\neps = %lf,PI=%lf\n",x,fun(x));
}
截圖如下
:
感謝你的閱讀,請用心感悟!希望可以幫到您!!分享也是一種快樂!!!請接力。。。