Question

The value of cos(x) is evaluated as follows: cos⁡(x)=1- x^2/2!+ x^4/4!- x^6/6!+ x^8/8!+⋯+ x^2n/((2n)!) Where n...

The value of cos(x) is evaluated as follows: cos⁡(x)=1- x^2/2!+ x^4/4!- x^6/6!+ x^8/8!+⋯+ x^2n/((2n)!) Where n is related to the accuracy level required for cos(x). Write a C++ program that asks the user to input n and x and then the program will evaluate cos(x) up to the 2n terms.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

#include<iostream>

#include<math.h>

using namespace std;

//method to calculate factorial

unsigned long long fact(unsigned long long n)

{

if(n==1)return 1;

return n*fact(n-1);

}

//method to compute cos(x)

double cos(unsigned long long n,double x)

{

double sum=1;

unsigned long i=2;

while(i<=(2*n))

{

double nume=pow(x,i);

unsigned long long d = fact(i);

//cout<<d<<endl;

//finding term value

nume = nume/d;

if(i%2==0)

{

sum=sum-nume;

}

else

{

sum=sum+nume;

}

i=i+2;

}

//returning sum

return sum;

}

int main()

{

//testing method

//reading input

unsigned long long n;

double x;

cout<<"Enter n and x:";

cin>>n>>x;

//displaying output

cout<<"cos("<<x<<") = "<<cos(n,x)<<endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
The value of cos(x) is evaluated as follows: cos⁡(x)=1- x^2/2!+ x^4/4!- x^6/6!+ x^8/8!+⋯+ x^2n/((2n)!) Where n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT