#include<stdio.h>
int main()
{
int i, n;
float x, sum, t;
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);
x=x*3.14159/180;
sum = t=x;
/* Loop to calculate the value of Sine */
for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
printf(" The value of Sin(%f) = %.4f\n",x,sum);
}
printf(" The value of Sin(%f) = %.4f",x,sum);
}
(https://codingconnect.net/wp-content/uploads/2016/01/sine-series1.png)
Hello guys in the above program
Let the value of x be 30.
(https://www.codingconnect.net/wp-content/uploads/2016/01/radian.png)
So, Radian value for 30 degree is 0.52359.
(https://www.codingconnect.net/wp-content/uploads/2016/01/sine.png)
but if i enter n=0 and x= 30 then the sum = 0.5236. But it should print the sum = 0.5236 for n=1 and x=30.because the first term in the series is x. why it prints 0.4997 instaed of 0.5236.
It is 2:30 in the morning so I'm only going to make a couple of suggestions.
#include<stdio.h>
int main()
{
int i, n;
float x, sum, t;
printf(" Enter the value for x : ");
scanf("%f",&x);
[/quote]
comments in your code are wonderful even better us variable names that are actually meaningful. a reader of your code should be ble to understand what a variable represents any place in your code.
[quote]
printf(" Enter the value for n : ");
scanf("%d",&n);
x=x*3.14159/180;
sum = t=x;
/* Loop to calculate the value of Sine */
for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
printf(" The value of Sin(%f) = %.4f\n",x,sum);
}
[/quote]
This would make more sense as a function. More importantly a function like this should contain a comment that expresses exactly what formula it is trying to implement. More importunity most programming languages come with factorial functions and power functions. Use those to implement your code first. If there is some reason not to; worry about it after getting the code to work.
[quote]
printf(" The value of Sin(%f) = %.4f",x,sum);
}
(https://codingconnect.net/wp-content/uploads/2016/01/sine-series1.png)
Hello guys in the above program
Let the value of x be 30.
(https://www.codingconnect.net/wp-content/uploads/2016/01/radian.png)
It isn't clear that your code even implements this.
So, Radian value for 30 degree is 0.52359.
(https://www.codingconnect.net/wp-content/uploads/2016/01/sine.png)
but if i enter n=0 and x= 30 then the sum = 0.5236. But it should print the sum = 0.5236 for n=1 and x=30.because the first term in the series is x. why it prints 0.4997 instaed of 0.5236.
Concentrate on order of operations or operator precedence. Like I said it is late.