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

double cosine(double x, int n) {
    double result = 1.0;
    double term = 1.0;
    int sign = -1;

    for (int i = 2; i <= n; i += 2) {
        term *= (x * x) / (i * (i - 1));
        result += sign * term;
        sign *= -1;
    }

    return result;
}

int main() {
    double angle;
    int terms;

    printf("Enter the angle in radians: ");
    scanf("%lf", &angle);

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

    double result = cosine(angle, terms);
    double standard_cos = cos(angle);

    printf("Approximated cos(%.2f) using %d terms: %.6f\n", angle, terms, result);
    printf("Actual cos(%.2f): %.6f\n", angle, standard_cos);

    return 0;
}

Embed on website

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