下列給定程序中函數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