problem 28


#include <stdio.h>

int main () {
  double x, sum = 0.0, term, factorial = 1.0;
  int terms, sign = 1;

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

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

  for (int i = 0; i < terms; i++) {
    term = sign * x * x * factorial / ((i + 1) * i);  // Combine calculations for efficiency
    sum += term;
    sign *= -1;
    factorial *= (i + 1);  // Update factorial efficiently
  }

  printf("The sum of the series for x = %.2lf and %d terms is: %.6lf\\n", x, terms, sum);

  return 0;
}



Contact Form