I have a program to find the Sin value of a given value(in radian) . It is working properly , but i want a better program or can u please give me a small explanation about my code . please ...

in my program

n=2; // what is this meaning

``````#include<iostream.h>
#include<math.h>
#include<conio.h>

void main()
{
float x,s=0,term,f;
int n,k;
cout<<"Enter the value:";
cin>>x;
k=x;
x=(x*22)/(7*180);
term = x;
cout<<x<<"\n";
cout<<sin(x)<<"\n";
fabs(s-(sin(x)));
n=2;
while(fabs(s-sin(x))>0.0001 )
{
s=s+term;
f=-(x*x)/(n*(n+1));
term = term*f;
n=n+2;
}
cout<<"Sin "<<k<<"="<<s;
getch();

}``````

5
Contributors
7
Replies
8
Views
9 Years
Discussion Span
Last Post by vijaysoft1

Did you actually write this code ?

You can use the `sin` function from the C++ library ...
(Look here for a reference)

22/7 is a poor approximation of pi.
You are essentially calculating the series x - (x^3)/3! + (x^5)/5! - (x^7)/7! + ... that is why n=2 at start and 2 is added every time 3+2=5 5+2=7 etc.
Don't know why k=x; is needed, for use in output you could use x as well.

>>22/7 is a poor approximation of pi.
Very right.
PI=3.14159265
22/7 =3.14285714
Your program is not doing good. What I meant to say is the aim of the program is not clear.
Moreover, you are using old headers, using void main.
Never use void main http://cppdb.blogspot.com/2009/02/should-i-use-void-main-or-int-main-or.html

I agree with Tux4life post about "Did you actually write this code ? "
As far as the Original problem is concerned sine can be easily calculated by the sin() function in cmath.
It takes radian as its measure.
Tux has already told you that.