Bài tập phương pháp lập trình pot - Pdf 18

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;

{
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));

//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;

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;

#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:

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<<"\nketqua: "<<"a>b"<<endl;
else
cout<<"\nketqua: "<<"a<b"<<endl;
}
return 0;
}
Bai 7:
//Giai PT bac nhat ax+b=0
#include<iostream.h>
void main()
{
int a,b;
cout<<"Nhap vao 2 so a,b: ";cin>>a>>b;
if(a==0)
if(b==0)
cout<<"\nPt co vo so nghiem "<<endl;
else
cout<<"Pt vo nghiem "<<endl;
else
cout<<"Pt co nghiem duy nhat x= "<<-b/a<<endl;
return 0;
}
Bai 8:
//Giai pt bac hai
#include<iostream.h>
void main()
{
int a,b,c,delta;
cout<<"Nhap a: ";cin>>a;
cout<<"Nhap b: ";cin>>b;

{
int t;
cout<<"Nhap vao thang t: ";cin>>t;
if(t==1||t==2||t==3)
cout<<"\nThang vua nhap vao thuoc quy 1"<<endl;
if(t==4||t==5||t==6)
cout<<"\nThang vua nhap vao thuoc quy 2"<<endl;
if(t==7||t==8||t==9)
cout<<"\nThang vua nhap vao thuoc quy 3"<<endl;
else
cout<<"\nThang vua nhap vao thuoc quy 4"<<endl;
return 0;
}
Bai 9.2:
//Nhap vao thang t, 1<=t<=12 cho bik t thuoc quy may trong nam
#include<iostream.h>
void main()
{
int t;
cout<<"Nhap vao thang t: ";cin>>t;
switch(t)
{
case 1:
case 2:
case 3:cout<<"\nThang vua nhap vao thuoc quy
1"<<endl;break;
case 4:
case 5:
case 6:cout<<"\nThang vua nhap vao thuoc quy
2"<<endl;break;

case 9:
case 11:cout<<"thang co 30 ngay"<<endl;break;
case 2:
cout<<"Nhap nam: ";cin>>nam;
if((nam%4==0)&&(nam%100!=0)) ngay=28; else
ngay=29;break;
cout<<"co:"<<ngay<<" ngay"<<endl;
}
return 0;
}
Bai 10.2:
//nhap thang t.cho bik thang t co bao nhiu ngay
#include<iostream.h>
void main()
{
int t,nam,ngay;
cout<<"Nhap thang t: ";cin>>t;
if(t==1||t==3||t==5||t==7||t==8||t==10||t==12)
cout<<t <<" Thang co 31 ngay"<<endl;
if(t==4||t==6||t==9||t==11)
cout<<"thang co 30 ngay"<<endl;
if(t==2)
cout<<"Nhap nam: ";cin>>nam;
if((nam%4==0)&&(nam%100!=0))ngay=28; else ngay=29;
cout<<"co:"<<ngay<<" ngay"<<endl;
return 0;
}
Bai 20:
//nhap cac so tu 1 den 7 xuat ra ca thu trong tuan.
#include<iostream.h>

if(kwh<=100)
tien=kwh*600;
else
if(kwh<=150)
tien=600*100+(kwh-100)*700;
else
if(kwh<=200)
tien=600*100+50*700+(kwh-150)*900;
else
tien=600*100+50*700+50*900+(kwh-200)*1100;
cout<<"tien dien la:"<<tien<<endl;
return 0;
}
Bai 22:
//nhap 3 canh. kiem tra co pahi la tam giac hay khong.
#include<iostream.h>
#include<math.h>
void main()
{
int a,b,c;
cout<<" Nhap canh a: "; cin>>a;
cout<<" Nhap canh b: "; cin>>b;
cout<<" Nhap canh c: "; cin>>c;
if(a+b>c&&b+c>a&&c+a>b)
{
cout<<"la tam giac"<<endl;
float p,cv,s;
cv=a+b+c;
p=cv/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));

}
Bai 24:
#include<iostream.h>
void main()
{
int n,a,b,c;
cout<<" Nhap cao so n co 3 chu so: "; cin>>n;
a=n%100;
b=(n/100)%10;
c=(n/100)/10;
if((a+b+c)%3==0)
cout<<"so "<< n << " la so nguyen duong chia het cho 3"<<endl;
else
cout<<"so "<< n << " khong chia het cho 3" <<endl;
return 0;
}
Bai 28:
//Nhap mot so n>=0. tinh Sn.
#include<iostream.h>
void main()
{
int i,n,s;
cout<<"Nhap so n= "; cin>>n;
s=0;
for(i=1;i<=n;i++)
s+=i;
cout<<s<<endl;
}
Bai 29:
//Nhap mot so n>=0. tinh Sn.

return 0;
}
Bai 31:
//nhap n. tinh sn
#include<iostream.h>
#include<conio.h>
void main()
{
float a,s;
int i,n;
cout<<"Nhap n= "; cin>>n;
s=0;
for (i=1;i<=n;i++)
{
a=(1.0/i);
s+=a;
}
cout<<s;
}
Bai 32:
#include <iostream.h>
void main()
{
int n,s;
cout<<"nhap n:";cin>>n;
s=0;
for(int i=1;i<=n;i++)
s=s+i*i;
cout<<"tong la:"<<s;
return 0;

BAI 1:
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[50];
cout<<"nhap vao sau s1:= ";
gets(a);
cout<<a<<endl;
int n,k;
n=strlen(a);
for(int i=0;i<n;i++)
{
k=a[i];
cout<<k<<endl;
}
cout<<a<<endl;
}
BAI 2:
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[50],k;
cout<<"Nhap vao sau S:= ";
gets(a);
cout<<"\n===============================\n";
cout<<a<<endl;

cout<<"\n===============================\n";
}
BAI 4:
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[50],k;
cout<<"Nhap vao sau S:= ";
gets(a);
cout<<"\n===============================\n";
cout<<a<<endl;
cout<<"\n===============================\n";
int n,s[30];
for(int i=65;i<91;i++)
s[i]=0;
n=strlen(a);
for(int i=0;i<n;i++)
for(int j=65;j<91;j++)
if(int(a[i])==j||int (a[i])==j+32)
s[j]++;
cout<<endl;
cout<<"So lan suat hien cua moi ky tu la: \n";
for(int i=65;i<91;i++)
if(s[i]!=0)
{
k=i;
cout<<(k)<<" : Suat Hien - "<<s[i]<<" - Lan"<<endl;
}


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status