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


 

 

#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) ;
}

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM