码迷,mamicode.com
首页 > 其他好文 > 详细

下列给定程序中函数fun的功能是:用下面的公式求π的近似值,直到最后一项的绝对值小于指定的数为止,π/4=1-1/3+1/5-1/7+...,例如,程序运行后,输入0.0001,程序输出3.1414

时间:2017-09-19 22:52:46      阅读:550      评论:0      收藏:0      [点我收藏+]

标签:float   clu   class   enter   div   bsp   运行   csharp   log   

 

 

#include <math.h>
#include <stdio.h>

float fun ( float num )
{   int s ;
    float n, t, pi ;

    t = 1 ; pi = 0 ; n = 1 ;  s = 1 ;
/**************found**************/
    while(fabs(t) >= num)
    {
        pi = pi + t ;
        n = n + 2 ;
        s = -s ;
/**************found**************/
        t = s/ n ;
    }
    pi = pi * 4 ;
    return pi ;
}

main( )
{   float n1, n2 ;

    printf("Enter a float number: ") ;
    scanf("%f", &n1) ;
    n2 = fun(n1) ;
    printf("%6.4f\n", n2) ;
}

  

 

下列给定程序中函数fun的功能是:用下面的公式求π的近似值,直到最后一项的绝对值小于指定的数为止,π/4=1-1/3+1/5-1/7+...,例如,程序运行后,输入0.0001,程序输出3.1414

标签:float   clu   class   enter   div   bsp   运行   csharp   log   

原文地址:http://www.cnblogs.com/jun699701/p/7554112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!