#include<stdio.h>
#include<math.h>
int factorial( int );
int main()

    int n;
    double x,i;
    double value, sinx;
    value = sinx = 0.0;

    printf("Enter the value for x and i");
    scanf("%lf %d", &x, &n);
    for( i=1; i<=n; i=i+2)
    {
        value = pow(x,i);
        sinx += (x - value / factorial(i));
    }

    printf("Result - %f", sinx );

    getchar();
    return 0;
}
int factorial( int n )
{
   int j, fact=1;
   for( j=n; j>0; j-- )
     fact = fact * j;

   return fact;
}


#include<stdio.h>
#include<math.h>
int factorial( int );
int main()

    int n;
    double x,i;
    double value, sinx;
    value = sinx = 0.0;

    printf("Enter the value for x and i");
    scanf("%lf %d", &x, &n);
    for( i=1; i<=n; i=i+2)
    {
        value = pow(x,i);
        sinx += (x - value / factorial(i));
    }

    printf("Result - %f", sinx );

    getchar();
    return 0;
}
int factorial( int n )
{
   int j, fact=1;
   for( j=n; j>0; j-- )
     fact = fact * j;

   return fact;
}


#include<stdio.h>
#include<math.h>
int factorial( int );
int main()

    int n;
    double x,i;
    double value, sinx;
    value = sinx = 0.0;

    printf("Enter the value for x and i");
    scanf("%lf %d", &x, &n);
    for( i=1; i<=n; i=i+2)
    {
        value = pow(x,i);
        sinx += (x - value / factorial(i));
    }

    printf("Result - %f", sinx );

    getchar();
    return 0;
}
int factorial( int n )
{
   int j, fact=1;
   for( j=n; j>0; j-- )
     fact = fact * j;

   return fact;
}

Dani AI

Generated

The code posted by aims for the Maclaurin series for sin(x) but has several concrete bugs and ambiguities: the summation term is formed incorrectly (it effectively keeps adding the raw x rather than the odd-power term with alternating sign), the loop index and loop bounds are unclear, factorial is an int (risk of overflow) and the implementation recomputes powers and factorials each iteration (inefficient). was correct to ask for clarification about what n should mean and what inputs/outputs are expected.

The correct Maclaurin series is:
sin(x) = sum_{k=0..∞} (-1)^k * x^(2*k+1) / (2*k+1)!
A stable, efficient implementation builds each term from the previous one instead of calling pow and a factorial routine every time. That also avoids integer overflow and keeps everything in double.

A compact, robust routine that computes the series by recurrence:

double sin_taylor(double x, int terms)
{
    double term = x;        /* first term (k=0): x */
    double sum = term;
    for (int k = 0; k < terms - 1; ++k) {
        term *= - (x * x) / ((2.0 * k + 2.0) * (2.0 * k + 3.0));
        sum += term;
    }
    return sum;
}

/* usage example:
   double s = sin_taylor(x_rad, 8);  // 'terms' = number of series terms
*/

Additional notes and checks: treat input x as radians (convert degrees with x_rad = x_deg * acos(-1.0) / 180.0 if needed); use double (or long double) everywhere for precision; prefer a tolerance-based stop (break when fabs(term) < 1e-12) rather than a fixed huge n; test against known values (e.g., x = pi/6 => 0.5). As suggested, stating whether n is a term count or a maximum exponent and giving sample input/output will clarify which variant is required.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.