B9:
#include<iostream.h>
#include<math.h>
main()
{
float x,e=0.1,y,gt;
int i;
cout<<"y= e^x = 1+ x/1! + x^2/2! +...+ x^i/i! +...\n";
cout<<"Nhap vao so thuc x = "; cin>>x; cout<<"\n";
cout<<"Sai so epsilon mac dinh = 0.1\n"; cout<<"\n";
i=1;
y=1;
gt=1;
while (fabs(pow(x,i)/gt) >= e)
{
y=y+pow(x,i)/gt;
i++;
gt=gt*i;
}
cout<<" y= e^x = "<<y<<"\n";
}
B10:
#include<iostream.h>
#include <math.h>
float pi=3.1415926535897932384626433832795;
float sinx()
{ float x,s,gt,e=0.0000001;
int i;
cout<<"Nhap goc x = "; cin>>x;
cout<<"Sai so epsilon mac dinh = 0.0000001\n";
cout<<"\n";
if (x>360) x=x-360;
x=x*pi/180;
s=0;
gt=1;
i=0;
while (fabs(pow(x,2*i+1)/gt) >= e)
{
s = s + pow(-1,i)*pow(x,(2*i+1))/gt;
i++;
gt=gt*(2*i)*(2*i+1);
}
cout<<"sin(x) = "<<s<<"\n";
}
void cosx()
{
float x,s,gt,e=0.0000001;
int i;
cout<<"Nhap goc x = "; cin>>x; cout<<"\n";
cout<<"Sai so epsilon mac dinh = 0.0000001\n";
cout<<"\n";
if (x>360) x=x-360;
x=x*pi/180;
i=0;
s=0;
gt=1;
while (fabs(pow(x,i)/gt) >= e)
{
s=s+pow(-1,i/2)*pow(x,i)/gt;
i += 2;
gt=gt*(i-1)*i;
}
cout<<"cos(x)= "<<s<<"\n";
}
void main()
{
int c;
cout<<"\n";
cout<<"----------------- Menu ---------------------"<<"\n";
cout<<"\n";
cout<<"1. Tinh sin(x)= x- x^3/3! + x^5/5! +...+(-1)^i*x^(2i+1)/(2n+1)! +..\n";
cout<<"\n";
cout<<"2. Tinh cos(x)= 1- x^2/2! + x^4/4! -... +(-1)^n*x^2n/(2n)! +...\n";
cout<<"\n";
cout<<"0. Thoat";
cout<<"\n";
cout<<"--------------------------------------------";
cout<<"\n";
cout<<"Chon muc : ";
cin>>c;
switch (c)
{
case 1 : sinx(); break;
case 2 : cosx(); break;
case 0 : break;
default: cout<<"Ban nhap sai=>thoat CT";
}
}
ok men