# Trying to code for calculating cosine series [closed]

I was trying to calculate cosine series : cos x = 1 - x^2/2! + x^4/4! - x^8/8! .... , where x is in radians: include iostream

include math.h

using namespace std;

long factorial (long num)

{

if (num >1)

return num*factorial(num-1);

else return 1;

}

int main()

{int X,sum=0;

cout<<"Enter value of x in radians : ";

cin>>X;

for (int i = 0; i<=4; ++i)

{

int z = pow(-1,i);

int p = (pow(X,2*i)*z)/factorial(2*i);

sum += p;

}


cout<

return 0;

} on putting x as 1.57( 90 degrees), I am getting 1, instead of 0. Can anyone explain why?

• Take a look at your question. Would you be able to read your code? If not, how do you expect us to? – Yuval Filmus Oct 16 '15 at 9:55
• @YuvalFilmus That's not very constructive. I'm pretty sure the asker would have formatted it legibly if they knew how to do that. However, since the question is off-topic (I know you know that, Yuval; I'm pointing it out for the asker's sake), the formatting doesn't make a lot of difference. – David Richerby Oct 16 '15 at 10:17
• @DavidRicherby I disagree. There are quite a few questions sporting this kind of illegible code, which for me shows a lack of respect for our community. The OP should try a bit harder. – Yuval Filmus Oct 16 '15 at 10:26
• @YuvalFilmus The policy is vote to close and comment in a constructive way. Please try to adhere to that. Even if you perceive a lack of respect (I'd recommend Hanlon's razor instead), it's a bad idea to respond in kind. – Raphael Oct 16 '15 at 10:30
• This question is offtopic here, as are all programming questions. If this is actually an algorithms question, please get rid of the source code, use pseudo code instead and explain your ideas, what you've tried to isolate the issue, and what questions remain. Regarding formatting, note the little question mark above the text box; it sends you so a detailed introduction to Markdown. – Raphael Oct 16 '15 at 10:31