Write a program that takes the value of positive integer N and outputs the sum of the first N good numbers.
#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;
}
Comments
Leave a comment