Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 40 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
40
Dung lượng
205 KB
Nội dung
BÀI TẬP CHƯƠNG 2: BAI 1: //Nhap ban kinh r. Tinh va xuat ra Chu Vi(CV) va Dien Tich(DT) //phan khai bao thu vien #include<iostream.h> void main() //phan khai bao bien { float r,cv,dt; cout<<"moi ban nhap r: "; cin>>r; //xuat ket qua cv=3.14*r*2; dt=3.14*r*r; cout<<"\nchu vi ban kinh duong tron r:"<<cv; cout<<"\nDien tich duong tron ban kinh r:"<<dt<<endl; return 0; } BÀI 2: //nhap canh a.tinh va xuat chu vi hinh vuong.*/ //khai bao thu vien #include<iostream.h> void main() { //khai bao bien float a,cv,dt; cout<<"Nhap canh hinh vuong a:"; cin>>a; //xuat ket qua cv=4*a; dt=a*a; cout<<"\n chu vi hinh vuong canh a la:"<<cv; cout<<"\n Dien tich hinh vuong canh a la:"<<dt<<endl; return 0; } BÀI 3: //Tinh chu vi va dien tich cua hinh chu nhat. #include<iostream.h> void main() { int a,b,cv,dt; cout<<"nhap a: ";cin>>a; cout<<"nhap b: ";cin>>b; cv=2*(a+b); dt=a*b; cout<<"Chu Vi HCN: "<<cv<<"\nDien Tich HCN: "<<dt<,endl; return 0; } BÀI 4. //nhap so n, tinh va xuat gia tri tuyet doi cau n. #include<iostream.h> void main() { int n; cout<<" Nhap so n: ";cin>>n; cout<<"ketqua= "<<abs(n)<<endl; return 0; } BÀI 5: //nhap a,h1,h2. tinh va xuat chu vi, dien tich hinh thoi #include<iostream.h> void main() { int a,h1,h2; cout<<"Nhap Canh a,h1,h2: "; cin>>a>>h1>>h2; //xuat ket qua cout<<"Chu Vi Hinh Thoi Canh a: "<<a*4<<"\nDien tich hinh thoi canh a: "<<1.0/2*(h1*h2)<<endl; return 0; } BÀI 6: #include <iostream.h> int main(int argc, char *argv[]) { int a,b,c,d,h; cout<<"nhap a,b,c,d,h:";cin>>a>>b>>c>>d>>h; float cv=(a+b+c+d); float dt=1.0/2*h*(a+b); cout<<"Chu vi Hinh Thang: "<<cv<<"\nDien Tich Hinh Thang: "<<dt<<endl; return 0; } BÀI 7: //nhap a,b,c. tinh va xuat chu vi, dien tich hinh tam giac #include<iostream.h> void main() { int a,b,c; cout<<"nhap 3 so a,b,c: ";cin>>a>>b>>c; float cv=a+b+c; float p=cv/2; float dt=sqrt(p*(p-a)*(p-b)*(p-c)); cout<<"chuvi hinh tam giac :"<<cv<<"\ndien tich hinh tam giac"<<dt<<endl; return 0; } BAI 8: //Nhap vao hai so nguyen duong a,b. tinh va xuat tong hieu tich thuong. #include<iostream.h> void main() { float a,b; cout<<"Nhap 2 so nguyen duong a, b: ";cin>>a>>b; //xuat ket qua cout<<"Tong= "<<a+b<<"\nHieu= "<<a-b<<"\nTich= "<<a*b<<"\nThuong= "<<float (a)/float (b)<<endl; return 0; } BÀI 9: //Nhap hai so Nguyen a,b. xuat ra a^b #include<iostream.h> #include<math.h> void main() { int a,b,ketqua; cout<<"Nhap vao hai so Nguyen a,b: ";cin>>a>>b; //xuat ketqua=pow(a,b); cout<<"ketqua a^b= "<<ketqua<<endl; return 0; } BÀI 10: //nhap so n, tinh va xuat can bac hai cua n #include<iostream.h> void main() { int n; cout<<" Nhap so n: ";cin>>n; cout<<"ketqua= "<<sqrt(n)<<endl; return 0; } BÀI 11: //nhap goc x,xuat sin x, cos x, tan x, cotg x #include<iostream.h> #include<math.h> void main() { int x; cout<<" Nhap goc x: ";cin>>x; float t=x*3.14/180; cout<<"sin(x)= "<<sin(t)<<"\ncos(x)= "<<cos(t)<<"\ntan(x)= "<<tan(t)<<"\ncotg(x)= "<<1/tan(t)<<endl; return 0; } BÀI 12: //Nhap toa do 2 diem A,B. tinh va xuat dod ai doan AB #include<iostream.h> #include<math.h> void main() { int x1,y1,x2,y2,AB; cout<<"nhap toa do diem a:";cin>>x1>>y1; cout<<"nhap toa do diem b:";cin>>x2>>y2; AB=sqrt(pow((x2-x1),2))+ (pow((y2-y1),2)); cout<<"Do dai AB= "<<AB<<endl; return 0; } BAI 13: //Nhap toa do 2 diem A,B. tinh va xuat he so goc cua duogn thang di qua hai diem AB #include<iostream.h> #include<math.h> void main() { float x1,y1,x2,y2; cout<<" Nhap toan do diem A(x1,y1): ";cin>>x1>>y1; cout<<" Nhap toan do diem B(x2,y2): ";cin>>x2>>y2; cout<<" He so goc cua duong thang AB = "<<float(y2-y1)/float(x2- x1)<<endl; return 0; } BAI 14: //Nhap vao so nguyen n va so thuc x. tinh va xuat ra bieu thuc A=(x^2+x+1)^n+ ( ) #include<iostream.h> #include<math.h> void main() { int n; float x; cout<<"Nhap vao so nguyen n: ";cin>> n; cout<<"Nhap vao so thuc x: ";cin>> x; //xuat float A=pow((pow(x,2)+x+1),n)+pow((pow(x,2)-x+1),n); cout<<"A= "<<A<<endl; return 0; } Bai 15: /* Nhap vao so giay bat ky t. tinh va xuat ra gio:phut:giay vidu:3750 thi xuat la 1:2:30 AM*/ #include<iostream.h> #include<time.h> void main() { int t,hour,minute,second; cout<<"Nhap so giay t: ";cin>>t; //xuat hour=(t/3600)%24; minute=(t%3600)/60; second=(t%3600)/60; cout<<"ketqua= "<<hour<<" : "<<minute<<" : "<<second<<endl; return 0; } Bài 16: //Nhap vao ngay thang nam va xuat ra dang ngay/thang/nam #include<iostream.h> void main() { int ngay,thang,nam; cout<<"Nhap ngay: ";cin>>ngay; cout<<"Nhap thang: ";cin>>thang; cout<<"Nhap nam: ";cin>>nam; cout<<"ket qua: "<<ngay<<"/"<<thang<<"/"<<nam<<endl; return 0; } Bai 17: //viet chuong trinh tinh loga(x) voi a,x la so thuc #include<iostream.h> #include<math.h> void main() { float a,x; cout<<" Nhap a(a>0 & a!=1): ";cin>>a; cout<<" Nhap x (x>0): ";cin>>x; cout<<"loga(x)= "<< log10(x)/log10(a)<<endl; return 0; } Bài 18: //nhap vao mot ky tu. In ra ma ASCII cua ky tu do, va In ra ky tu ke tiep no #include<iostream.h> void main() { char kt; cout<<"Nhap vao Ky tu bat ky: ";cin>> kt; //xuat cout<<"Ma ASCII cua ky tu tren la: "<<int(kt); cout<<"\nMa ASCII ky tu ke tiep ky tu tren la: "<<int(kt+1)<<endl; return 0; } Bai 19: //Nhap vao ba diem mon toan, ly hoa. in ra diem trung binh voi hai so le thap phan #include<iostream.h> #include<math.h> #include<iomanip.h> void main() { float toan,ly,hoa; cout<<"NHap diem mon toan: ";cin>>toan; cout<<"NHap diem mon ly: ";cin>>ly; cout<<"NHap diem mon hoa: ";cin>>hoa; cout<<"ket qua: "<<setprecision(3)<<((toan+ly+hoa)/3)<<endl;//co cach lam khac printf("ketqua:%0.2f",(toan+ly+hoa)/3) return 0; } Bai 20: //viet chuong trinh do nguoc so nguyen duong co dung 3 ky so. #include<iostream.h> void main() { int n, m, k; cout << "Nhap n: "; cin >> n; k = n; m = 0; while(k > 0) { m = m*10+ k%10; k = k / 10; } cout << "So dao nguoc cua " << n << " la: " << m<<endl; return 0; } Chuong 3 Bai 1: //Nhap 1 so n >=0. tinh va xuat can bac hai cua n #include<iostream.h> void main() { int n; cout<<"Nhap vao so n: ";cin>>n; if(n>=0) cout<<"can bac hai cua n la: "<<sqrt(n)<<endl; else cout<<"n khong co can bac hai"<<endl; return 0; } Bai 2: //Nhap vao so giay bat ky t>=0. tinh va xuat ra dang Gio:Phut:Giay #include<iostream.h> void main() { int t,h,p,g; cout<<"NHap vao so giay t bat ky: ";cin>>t; if((t/3600)%24<12) { h=(t/3600)%24; p=(t%3600)/60; g=(t%3600)%60; cout<<"\nKetqua(gio:phut:giay)= "<<h<<":"<<p<<":"<<g<<" AM"<<endl; } else { h=((t/3600)%24)-12; p=(t%3600)/60; g=(t%3600)%60; cout<<"\nKetqua(gio:phut:giay)= "<<h<<":"<<p<<":"<<g<<" PM"<<endl; } return 0; } Bai 3: //Nhap so thuc a,b,c. xuat tim so max #include <iostream.h> int main(int argc, char *argv[]) { float a,b,c,max; cout<<"nhap a,b,c:";cin>>a>>b>>c; max=a; if(b>max) max=b; if(c>max) max=c; cout<<"so lon nhat la:"<<max; return 0; } Bai 4: //nhap vao so n.xuat ran chan va n le. #include<iostream.h> void main() { int n; cout<<"Nhap n :";cin>>n; if(n%2==0) cout<<"\nket qua: "<<"n la so chan"<<endl; else cout<<"\nket qua: "<<"n la so le"<<endl; return 0; } [...]... cout . BÀI TẬP CHƯƠNG 2: BAI 1: //Nhap ban kinh r. Tinh va xuat ra Chu Vi(CV) va Dien Tich(DT) //phan khai. r:"<<cv; cout<<"
Dien tich duong tron ban kinh r:"<<dt<<endl; return 0; } BÀI 2: //nhap canh a.tinh va xuat chu vi hinh vuong.*/ //khai bao thu vien #include<iostream.h> void. la:"<<cv; cout<<"
Dien tich hinh vuong canh a la:"<<dt<<endl; return 0; } BÀI 3: //Tinh chu vi va dien tich cua hinh chu nhat. #include<iostream.h> void main() { int