求e^x的值:根据以下公式求e^x的近似值,要求累加到某项的绝对值小于1e-6时为止。

在这里插入图片描述

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

void main() {
    int i, j, x;
    double s, t1, t2;
    scanf("%d", &x);
    s = 1;
    t1 = x;
    t2 = 1;
    i = 1;
    while (fabs(t1 / t2) >= 1e-6) {
        s += t1 / t2;
        i++;
        t1 = 1;
        t2 = 1;
        for (j = 1; j <= i; j++) {
            t1 *= x;
            t2 *= j;
        }
    }
    printf("%lf", s);
}

猜你喜欢

转载自blog.csdn.net/qq_38490457/article/details/105174838