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

double ln_taylor_series(double x, int n) {
    if (x <= -1) {
        printf("Natural logarithm is not defined for x <= -1.\n");
        return -1;
    }

    double result = 0.0;
    for (int i = 1; i <= n; i++) {
        int sign = (i % 2 == 0) ? -1 : 1; 
        double term = pow(x, i) / i; 
        result += sign * term;
    }
    return result;
}

int main() {
    double x, result;
    int terms;

    printf("Enter the value of x: ");
    scanf("%lf", &x);

    if (x <= -1) {
        printf("Natural logarithm is not defined for x <= -1.\n");
        return 1;
    }

    printf("Enter the number of terms: ");
    scanf("%d", &terms);

    result = ln_taylor_series(x, terms);

    printf("The natural logarithm of (1 + %.2f) using %d terms is approximately: %.6f\n", x, terms, result);

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: