write a c++ program to compute the cosine series | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

write a c++ program to compute the cosine series

cos(x) = 1-x^2 / 2! + x^4 / 4! - x^6 / 6!+...x^n / n!

13th Feb 2017, 8:41 AM
akashlina dewanjee
akashlina dewanjee - avatar
2 Answers
+ 7
int factorial( int n ) { int fact=1; for(int I=1;I<=n;I++) fact*=I; return fact; } int pow(int a,int b) { int power=1; for(int I=1;I<=b;i++) power*=a; return power; } float cosine(int x,int n) { int sign=1; float cos=0.0; for(int I=0; I<=n;I+=2,sign*=-1) cos+=(pow(x,I))/factorial(I)*sign; return cos; } //change data types of you require //sorry for semicolons missing and undeclared variables if any 😑😑😑
13th Feb 2017, 1:15 PM
Megatron
Megatron - avatar
+ 4
Thanks a lot!
13th Feb 2017, 2:35 PM
akashlina dewanjee
akashlina dewanjee - avatar