2014-03-02 62 views
0

我有一個關於C編程的問題計算用戶n值的e值?

我想寫一個程序爲用戶輸入計算e值輸入n個值。

你知道;我們可以定義n = 1,2的x = pow(1 + 1/n,n)。從數學上可以看出x-> e爲n - >無窮大。

我該怎麼做?

我還沒有這樣做,我試過,但就像我說我沒有工作:

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

int main() 
{ 
    int i,n,x1; 

    printf("Enter a n value:"); 
    scanf("%d", &n); 

     for (i = 0;; i++) 
     { 
      x1 = pow((1 + 1/n), n); 
      printf("Values:%d",x1); 
     } 
    system("pause"); 
    return 0; 
} 
+0

你有沒有條件的無限循環,而且絕不使用循環變量'我'在循環內部,而不是你一遍又一遍地重複同樣的'n'。 – Arkku

+0

...和無盡循環結束後,暫停。只是要確定。 – Potatoswatter

回答

1
1/n 

是兩個整數操作數的表達式。所以執行整數除法。對於n等於1,此評估爲1。對於大於1的所有n的值,該整數除法評估爲0

您想要浮點除法,所以必須至少使其中一個操作數成爲浮點值。例如

1.0/n 

您還需要申報x1是一個浮點值,並使用%f。嘗試使用整數變量來近似e並不好。

我想你會在某個時候需要實現一個循環終止條件。就目前而言,你的循環是毫無意義的,因爲循環內部使用的值都不會改變。

這裏有一個程序,也許是在朝着正確的方向:

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

int main(void) 
{ 
    for (int n = 1; n <= 1000; n++) 
    { 
     double e = pow(1 + 1.0/n, n); 
     printf("n=%d, approximation to e=%.16f\n", n, e); 
    } 
    printf("true value of e=%.16f\n", exp(1.0)); 
    return 0; 
} 

輸出

 
n=1, approximation to e=2.0000000000000000 
n=2, approximation to e=2.2500000000000000 
n=3, approximation to e=2.3703703703703698 
n=4, approximation to e=2.4414062500000000 
n=5, approximation to e=2.4883199999999994 
n=6, approximation to e=2.5216263717421135 
n=7, approximation to e=2.5464996970407121 
n=8, approximation to e=2.5657845139503479 
n=9, approximation to e=2.5811747917131984 
n=10, approximation to e=2.5937424601000023 
.......... 
n=991, approximation to e=2.7169116115768883 
n=992, approximation to e=2.7169129915688766 
n=993, approximation to e=2.7169143687840753 
n=994, approximation to e=2.7169157432307069 
n=995, approximation to e=2.7169171149169880 
n=996, approximation to e=2.7169184838514693 
n=997, approximation to e=2.7169198500421694 
n=998, approximation to e=2.7169212134981109 
n=999, approximation to e=2.7169225742266474 
n=1000, approximation to e=2.7169239322355936 
true value of e=2.7182818284590451 

這是相當有趣的是收斂的速度是真窮。估計的準確性永遠不會好,因爲對於大型n,您將在1.0 + 1.0/n中遭受四捨五入。這絕對不是一種有效的方式來近似e

這個版本,使用infinite sum,收斂得更快:

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

int main(void) 
{ 
    double e = 0.0; 
    double increment = 1.0; 
    for (int n = 0; n <= 20; n++) 
    { 
     e += increment; 
     increment /= (n+1); 
     printf("n=%d, approximation to e=%.16f\n", n, e); 
    } 
    printf("true value of e=%.16f\n", exp(1.0)); 
    return 0; 
} 

輸出

 
n=0, approximation to e=1.0000000000000000 
n=1, approximation to e=2.0000000000000000 
n=2, approximation to e=2.5000000000000000 
n=3, approximation to e=2.6666666666666665 
n=4, approximation to e=2.7083333333333330 
n=5, approximation to e=2.7166666666666663 
n=6, approximation to e=2.7180555555555554 
n=7, approximation to e=2.7182539682539684 
n=8, approximation to e=2.7182787698412700 
n=9, approximation to e=2.7182815255731922 
n=10, approximation to e=2.7182818011463845 
n=11, approximation to e=2.7182818261984929 
n=12, approximation to e=2.7182818282861687 
n=13, approximation to e=2.7182818284467594 
n=14, approximation to e=2.7182818284582302 
n=15, approximation to e=2.7182818284589949 
n=16, approximation to e=2.7182818284590429 
n=17, approximation to e=2.7182818284590455 
n=18, approximation to e=2.7182818284590455 
n=19, approximation to e=2.7182818284590455 
n=20, approximation to e=2.7182818284590455 
true value of e=2.7182818284590451