2018/12/19求π的近似值

本题要求编写程序,根据下式求π的近似值,直到最后一项小于给定精度eps。
在这里插入图片描述
输入格式:

输入在一行中给出精度eps,可以使用以下语句来读输入:
scanf("%le", &eps);
输出格式:

在一行内,按照以下格式输出π的近似值(保留小数点后5位):
PI = 近似值
输入样例:

1E-5
输出样例:

PI = 3.14158

#include <stdio.h>
 
int main () {
    double eps,sum=2,i,temp=1;
    scanf("%le",&eps);
    for ( i=1; temp>eps; i++){
        temp *=  i /(2*i+1);
        sum += temp*2 ;
    }
    printf("PI = %.5f",sum);
    return 0 ; 
}

猜你喜欢

转载自blog.csdn.net/qq_44149969/article/details/85109214
今日推荐