I've been given an assignment to approximate the value of sine of an angle in degrees by using the formula: sin(x) = x −x3/3!+x5/5!−x7/7!+x9/9!· · ·
x is an angle in radians. He wants the program outline to be like so:
1 read in a new value for angle
2 while(angle != 0){
3 convert angle to radians (multiply angle by pi/ 180 )
4 sine = 0
5 n = 1
6 while(n ≤ 15) {
7 calculate next term of series and add it to/subtract it from sine
8 increment n
9 }
10 display sine
11 read in a new value for angle
12 }
Here is what i wrote:
Im not getting the right value. I think my problem lies in the s variable. how would i write that. I made it so n would increment [n++] and it would switch from positive to negative like the formula [(pow(-1,n+1)]. It looks right. Can someone help me figure that part out? Thanks
x is an angle in radians. He wants the program outline to be like so:
1 read in a new value for angle
2 while(angle != 0){
3 convert angle to radians (multiply angle by pi/ 180 )
4 sine = 0
5 n = 1
6 while(n ≤ 15) {
7 calculate next term of series and add it to/subtract it from sine
8 increment n
9 }
10 display sine
11 read in a new value for angle
12 }
Here is what i wrote:
Code:
#include<math.h>
#include<stdio.h>
#define PI 3.141592653589793
int main()
{
float angle,radian,sine,n,x,factorial,s,i;
x = 1;
printf("Enter angle in degrees (0 to quit)\n");
scanf("%f",&angle);
while (angle!=0){
radian=angle*(PI/180);
sine=0;
n=1;
while (n<=15)
{
s = (radian + (pow(-1,n+1))+(pow(radian, n)) / (n));
n++;
}
printf("sin(%f)=%f\n",angle,s);
printf("Enter angle in degrees (0 to quit): ");
scanf("%f",&angle);
}
}
return 0;
}
Comment