Answer to Question #159667 in C for Niharika

Question #159667

Write a C program that accepts a positive integer n and a real number x from the keyboard

and prints out the sum of the n terms of the series

sin(x) = X∞

n=0

tn, tn = (−1)n x

2n+1

(2n + 1)!


1
Expert's answer
2021-01-29T03:42:13-0500
#include <stdio.h>
#include <math.h>

int main() {
    double x, s, tn;
    int n, i;
    
    printf("Enter x: ");
    scanf("%lf", &x);
    printf("Enter n: ");
    scanf("%d", &n);
    
    /* The first term */
    tn = x;     
    s = tn;
    
    for (i=1; i<n; i++) {
        /* Next term */
        tn = -tn * x*x / (2*i) / (2*i + 1);
        s += tn;
    }
    
    printf("Approximation to sin(%f) with %d terms is %f (real value is %f)\n",
           x, n, s, sin(x));

    return 0;           
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS