It has to be in c please Thanks No arrays intro c class Writ
It has to be in c++ please. Thanks (No arrays---- intro c++ class)
Write a function to compute the cosine of x. The user should supply x and a positive integer n. We compute the cosine of x using the series and the computation should use all terms in the series up through the term involving x^n cos x = 1 - x^2/2! + x^4/4! - x^6/6! -..... Write a function to compute sinx for given x. The user should supply x and a positive integer n. We compute the sine of x using the series and the computation should use all terms in the series up through the term involving x^n sin x = x - x^3/3! + x^5/5! - x^7/7! + x^9/9!Solution
Sine series:
#include<iostream> using namespace std;
int main()
{
int i,j,n,fact,sign=-1;
float x, p,sum=0; cout<<\"Enter the value of x : \";
cin>>x;
cout<<\"Enter the value of n : \";
cin>>n;
for(i=1;i<=n;i+=2)
{
p=1;
fact=1;
for(j=1;j<=i;j++) { p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum+=sign*p/fact;
}
cout<<\"sin\"<<x<<\"=\"<<sum;
return 0;
}
Cosine series:
#include<iostream> using namespace std;
int main()
{
int i,j,n,fact,sign=-1;
float x, p,sum=0;
cout<<\"Enter the value of x : \";
cin>>x; cout<<\"Enter the value of n : \";
cin>>n; for(i=2;i<=n;i+=2)
{
p=1
fact=1;
for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sum+=sign*p/fact; sign=-1*sign;
}
cout<<\"cos \"<<x<<\"=\"<<1+sum; return 0;
}
