我得到编译但是0作为我的答案

时间:2016-02-18 01:19:38

标签: c

计算e ^ x,但我对编程很陌生。我之前做过两个节目。我修复了shell指出的任何错误,一旦运行就会变为零。

我有这个:

#include <stdio.h>   

int main (void) {
    float answer = 1.0, x, next_term, n;
    int power;

    printf("Enter x: ");
    scanf("%f", &x);
    printf("Enter the number of terms: ");
    scanf("%f", &n);

    next_term = x;
    for (power = 1; power <= n ; power = power + 1) {
        answer = answer * next_term;
        next_term = next_term * x / (power + 1);

    }
    printf("exp(%f) = %f\n", x, answer);

    return 0;
}

输出:

  

exp(8.000000)= 0.000000

1 个答案:

答案 0 :(得分:2)

如果您正在计算无限系列,则需要添加这些字词,而不是将它们相乘......即answer=answer+next_term;