求π/2近似值的公式为π/2=2/1*2/3*4/3*4/5*...*2n/(2n-1)*2n/(2n+1)
式中n=1,2,3...设计一个程序,求出n=1000时π的近似值
求π/2近似值的公式为π/2=2/1*2/3*4/3*4/5*...*2n/(2n-1)*2n/(2n+1)
式中n=1,2,3...设计一个程序,求出n=1000时π的近似值
方法一:
double m, k = 1;
double n;
for (n = 1; n <= 1000; n++)
{
m = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1));
k *= m;
m = 1;
}
Console.Write("{0}", k);
Console.Read();
方法二:
double n, i = 1, j = 1, k = 1;
for (n = 1; n <= 1000; n++)
{
i = i * (2 * n);
j = j * (2 * n - 1);
k = k * (2 * n + 1);
}
double a;
a = i / (j * k);
Console.Write("{0},{1},{2},{3}", a, i, j, k);
Console.Read();
#include<math.h> #include<stdio.h>
void main() { double fenzi=1,fenmu=1; double sum=0,pi; while(fenmu<=100) { sum=sum+fenzi/fenmu; fenzi=-1*fenzi; fenmu=fenmu+2; } pi=sum*4; printf("%f",pi); }