#include <stdio.h>
#include <math.h>
double calculateNaturalLog(double x, int n) {
if (x <= -1) {
printf("Error: Natural logarithm is undefined for x <= -1.\n");
return 0;
}
double result = 0.0;
double term = x;
int sign = -1;
for (int i = 2; i <= n; i++) {
term *= (x * -1);
result += sign * (term / i);
sign *= -1;
}
return result;
}
int main() {
double x;
int n;
printf("Enter the value of x: ");
scanf("%lf", &x);
if (x <= -1) {
printf("Error: Natural logarithm is undefined for x <= -1.\n");
return 1;
}
printf("Enter the number of terms (n): ");
scanf("%d", &n);
double result = calculateNaturalLog(x, n);
double actualResult = log(1 + x);
printf("Approximation of ln(1 + %.2lf) using %d terms: %.6lf\n", x, n, result);
printf("Actual ln(1 + %.2lf) using math.h: %.6lf\n", x, actualResult);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: