You are on page 1of 25

41.

Vit chng trnh tnh din tch v chu vi ca hnh ch nht vi chiu di v
chiu rng c nhp t bn phm.
#include <iostream.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[])
{
float a,b,s,p;
cout<<"nhap so a"<<endl;
cin>>a;
cout<<"nhap so b"<<endl;
cin>>b;
s=a*b;
p=(a+b)*2;
cout<<"dien tich hinh chu nhat= "<<s<<endl;
cout<<"chu vi hinh chu nhat= "<<p<<endl;
return 0;
}

42. Vit chng trnh tnh din tch v chu vi hnh trn vi bn knh c nhp
t bn phm.
#include <iostream.h>

#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[])
{
float r,s,p;
cout<<"nhap so r"<<endl;
cin>>r;
p=r*2*3.14;
s=r*r*3.14;
cout<<"dien tich hinh tron = "<<s<<endl;
cout<<"chu vi hinh tron = "<<p<<endl;
return 0;
}

43. Nhp s nguyn dng n (n>0). Lit k tt c cc s nguyn t nh hn n.


#include <iostream.h>
#include <stdio.h>
#include <math.h>
void main()
{
int n,dem=0;
cout<<"nhap so "<<endl;
cin>>n;
for (int i=2;i<=n;i++)

{
for (int j=1;j<=i;j++)
{
if(i%j==0)
dem=dem+1;
}
if(dem==2)
cout<< so nguyen to nho hon <<n<<= << i<<endl;
dem=0;
}
return 0;
}

44. Nhp snguyn dng n (n>0). Lit k n s chnh phng u tin.


#include <iostream.h>
int main(int argc, char *argv[])
{
int n,j,i;
cout<<"nhap so "<<endl;

cin>>n;
for(i=1; i<=n; i++)
{
j=1;
while(j*j<i)
j+=1;
if(j*j==i)
cout<<i<<""<<endl;
}
cout<<endl;
return 0;
}

Bi 45: Nhp s nguyn dng n (n>0). m xem c bao nhiu s hon thin < n.
#include <iostream>
using namespace std;
void Nhap(int &a)

{
cout<<"Nhap so nguyen n : ";
cin>>a;
}
bool KiemTraHT(int n)
{
int tong=0;
for (int i=1;i<n;i++)
if (n%i==0)
tong=tong+i;
if (tong ==n)
return true;
return false;
}
int DemSoHT(int n)
{
int dem=0;
for(int i=1;i<=n;i++)

if(KiemTraHT(i))
dem++;
cout<<dem<<endl;
return dem;
}
int main()
{
int n;
Nhap(n);
cout<<"So hoan thien < n la : "<<endl;
DemSoHT(n);
cout<<endl;
}

46. Nhp s nguyn dng n (0 <= n< 1000) v in ra cch c ca n.


#include <iostream>
using namespace std;
void Nhap(int &a)
{

cout<<"Nhap so nguyen a:";


cin>>a;
}
void DocSo(int n)
{
int tram=n/100;
int chuc=n/10%10;
int dv=n%10;
switch(tram)
{
case 1: cout<<"Mot Tram ";break;
case 2: cout<<"Hai Tram ";break;
case 3: cout<<"Ba Tram ";break;
case 4: cout<<"Bon Tram ";break;
case 5: cout<<"Nam Tram ";break;
case 6: cout<<"Sau Tram ";break;
case 7: cout<<"Bay Tram ";break;
case 8: cout<<"Tam Tram ";break;
case 9: cout<<"Chin Tram ";break;
}
switch(chuc)
{
case 0: if (dv==0)
{cout<<" ";break;}
else
{cout<<"Le ";break;}
case 1: cout<<"Muoi ";break;
case 2: cout<<"Hai Muoi ";break;
case 3: cout<<"Ba Muoi ";break;

case 4: cout<<"Bon Muoi ";break;


case 5: cout<<"Nam Muoi ";break;
case 6: cout<<"Sau Muoi ";break;
case 7: cout<<"Bay Muoi ";break;
case 8: cout<<"Tam Muoi ";break;
case 9: cout<<"Chin Muoi ";break;
}
switch(dv)
{
case 5:if (chuc==0)
{cout<<"Lam ";break;}
else
{cout<<"Nam ";break;}
case 1: cout<<"Mot"<<endl;break;
case 2: cout<<"Hai"<<endl;break;
case 3: cout<<"Ba"<<endl;break;
case 4: cout<<"Bon"<<endl;break;
case 6: cout<<"Sau"<<endl;break;
case 7: cout<<"Bay"<<endl;break;
case 8: cout<<"Tam"<<endl;break;
case 9: cout<<"Chin"<<endl;break;
}
}
int main()
{
int(n);
Nhap(n);
DocSo(n);

47. Vit chng trnh tnh tin thu my dch vInternet v in ra mn hnh kt
qu. Vi dliu nhp vo l gibt u thu (GBD), gikt thc thu
(GKT), s my thu (SoMay).
- iu kin cho dliu nhp: 6<=GBD<GKT<=21. Gi l s nguyn.
- n gi: 2500cho mi gimy trc 17:30 v 3000cho mi gi my
sau 17:30.
#include <iostream.h>
#include <math.h>
int main(int argc, char *argv[])
{
int gbd,gkt,sotien;
do
{
cout<<"nhap gbd"<<endl;
cin>>gbd;
}
while(gbd<6 || gbd>21);
do
{
cout<<"nhap gio ket thuc"<<endl;

cin>>gkt;
}
while(gkt< 11 || gkt>21 || gkt<gbd );
//gio bat dau va ket thuc tu 6 gio den 21 gio//
if(gbd<=17 && gkt<=17 )
sotien=2500*(gkt-gbd);
//gio bat dau gio ket thuc sau 17 gio//
if(gbd>17 && gkt>17)
sotien=3000*(gkt-gbd);
//gio vao<=17 gio va ket thuc >17 gio//
if(gbd <=17 && gkt >17)
sotien=(17-gbd)*2500+(gkt-17)*3000;
cout<<"tong so tien= "<<sotien<<endl;
return 0;
}

48. Vit chng trnh tnh tin lng ngy cho cng nhn, cho bit trc gi
vo ca, gi ra ca ca mi ngi.
Bit rng:
- Tin tr cho mi gi trc 12 gi l 6000 v sau 12 gi l 7500.
- Gi vo ca sm nht l 6 gi sng v gi ca tr nht l 18 gi(Gi
s gi nhp vo nguyn).

#include <iostream.h>
#include <math.h>
int main(int argc, char *argv[])
{
int n,i,a,ra,vao,sotien;
do
{
cout<<"nhap vao gio vao "<<endl;
cin>>vao;
}
while(vao<6 || vao>18);
do
{
cout<<"nhap vao gio ra "<<endl;
cin>>ra;
}
while(ra<6 || ra>18 || ra<vao);
//gio vao va gio ra tu 6 den 12 gio
if(ra<=12 && vao<=12)
sotien=6000*( ra - vao );
//gio vao va gio ra sau 12 h
if(ra>12 && vao>12)
sotien=7500*(ra-vao);
//gio vao <=12 va gio ra >12
if(vao<=12 && ra>12)
sotien=(12-vao)*6000+(ra-12)*7500;
cout<<"so tien phai tra la: "<<sotien<<endl;
return 0;

50. Nhp vo 3 sthc a, b, c v kim tra xem chng c thnh lp thnh 3 cnh
ca mt tam gic hay khng? Nu c hy tnh din tch, chiu di mi
ng cao ca tam gic v in kt qu ra mn hnh.
- Cng thc tnh din tch s = sqrt(p*(p-a)*(p-b)*(p-c) )
- Cng thc tnh cc ng cao: ha = 2s/a, hb=2s/b, hc=2s/c.
(Vi p l na chu vi ca tam gic).
#include <iostream.h>
int main(int argc, char *argv[])
{
int a,b,c;
float p,s,ha,hb,hc;
cout<<"nhap so "<<endl;
cin>>a;
cout<<"nhap so "<<endl;

cin>>b;
cout<<"nhap so "<<endl;
cin>>c;
if(a=b=c)
cout<<"la tam giac"<<endl;
else
cout<<"khong phai la tam giac"<<endl;
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
ha=(2*s)/a;
hb=(2*s)/b;
hc=(2*s)/c;
cout<<"dien tich tam giac = "<<s<<endl;
cout<<"chieu cao canh a ha="<<ha<<endl;
cout<<"chieu cao canh b hb="<<hb<<endl;
cout<<"chieu cao canh c hb="<<hc<<endl;
return 0;
}

51. Nhp vo 6 sthc a, b, c, d, e, f . Gii h phng trnh sau :

#include <iostream.h>
#include <math.h>
int main(int argc, char *argv[])
{
float a,b,c,d,e,f;
float x,y;
float dx,dy,dd;
cout<<"nhap so a"<<endl;
cin>>a;
cout<<"nhap so b"<<endl;
cin>>b;
cout<<"nhap so c"<<endl;
cin>>c;
cout<<"nhap so d"<<endl;
cin>>d;

cout<<"nhap so e"<<endl;
cin>>e;
cout<<"nhap so f"<<endl;
cin>>f;
dd=a*e-d*b;
dx=c*e-b*f;
dy=a*f-d*c;
x=dx/dd;
y=dy/dd;
if(dd==0)
{
if(dx==0 && dy==0)
cout<<"phuong trinh vo so nghiem"<<endl;
else
cout<<"phuong trinh vo nghiem"<<endl;
}
else
{
cout<<"phuong trinh co nghiem La: "<<endl;
cout<<"x= "<<x<<endl;
cout<<"y= "<<y<<endl;
}
return 0;
}

52. Vit chng trnh nhp 2 s nguyn dng a, b. Tm USCLN v BSCNN


ca hai s nguyn .
#include <iostream.h>
int main(int argc, char *argv[])
{
int a,b,c,d,ucln,bcnn;
cout<<"nhap so a"<<endl;
cin>>a;
cout<<"nhap so b"<<endl;
cin>>b;
c=a;
d=b;
while(a!=b)
{
if(a>b)

a-=b;
else
b-=a;
ucln=a;
}
cout<<"uoc chung lon nhat= "<<ucln<<endl;
bcnn=(c*d)/ucln;
cout<<"boi chung nho nhat= "<<bcnn<<endl;
return 0;
}

53. Vit chng trnh tnh tng nghch o ca n giai tha.


#include <iostream.h>
#include <math.h>
int main(int argc, char *argv[])
{
int n;
float tong,gt;
cout<<"nhap so "<<endl;
cin>>n;
tong=0;

gt=1;
for(int i=1; i<=n; i++)
{
gt=gt*i;
tong=tong+1/gt;
}
cout<<"tong nghich dao giai thua= "<<tong<<endl;
return 0;
}

54. Cho 2 s nguyn a, b. Vit hm hon v gi tr 2 s trn.


#include <iostream.h>
int main(int argc, char *argv[])
{
int a,b,max;
cout<<"nhap so "<<endl;
cin>>a;
cout<<"nhap so"<<endl;
cin>>b;
if(a>b)
{
max=a;

a=b;
b=max;
}
else
{
max=b;
b=a;
a=max;
}
cout<<"hoan vi cua hai so la "<<a<<b<<endl;
return 0;
}

55. (*)Vit chng trnh nhp s nguyn dng n gm 5 ch s, kim tra xem
cc ch s n c phi l s i xng hay khng.
V d: i xng: 13531
Khng i xng: 13921
#include <iostream.h>
int main(int argc, char *argv[])
{
long int n,so,sodao=0;
cout<<"nhap so "<<endl;
cin>>n;

so=n;
while(so!=0)
{
sodao=sodao*10+so%10;
so=so/10;
}
if(sodao==n)
cout<<"la so doi xung"<<endl;
else
cout<<"khong la so doi xung"<<endl;
return 0;
}

57. Vit chng trnh nhp s nguyn dng n gm k ch s, (0<=k<=5) m


xem n c bao nhiu ch s l s nguyn t.
#include <iostream.h>
int main(int argc, char *argv[])
{
int i,a,n,dem,demt=0;
cout<<"nhap so"<<endl;
cin>>n;
do
{

a=n%10;
n=n/10;
dem=0;
for(i=1; i<=a; i++)
if(a%i==0)
dem++;
if(dem==2)
demt++;
}
while(n!=0);
cout<<"demsont= "<<demt<<endl;
return 0;
}
58.
#include <iostream.h>
int tonguocso(int n);
int main(int argc, char *argv[])
{
int n;
cout<<"tinh tong cac uoc so"<<endl;
cout<<"nhap so can tinh "<<endl;
cin>>n;
while(n<0 || n>99999)
{
cout<<"so N co toi da 5 chu so0"<<endl;
cout<<"Nhap lai so n can kiem tra "<<endl;

cin>>n;
}
cout<<"so "<<n<<" co tong cac uoc so "<<tonguocso(n)<<endl;
}
int tonguocso(int n)
{
int tong=0;
for(int i=1; i<=n; i++)
{
if(n%i==0) tong+=i;
}
return tong;
}

60. Vit chng trnh nhp s nguyn dng n gm k ch s (0<=k<=5) , kim


tra xem cc ch s ca n c ton l hay ton chn khng.?
#include <iostream.h>
#include <math.h>
void nhap(int M[5], int &n);
int toanchan(int M[5], int n);
int main(int argc, char *argv[])
{
int M[5];
int n;

nhap(M,n);
toanchan(M,n);
if(toanchan(M,n))
cout<<"mang toan chan"<<endl;
else
cout<<"mang toan le"<<endl;
return 0;
}
void nhap(int M[5], int &n)
{
cout<<"nhap mang"<<endl;
cin>>n;
for(int i=0; i<n; i++)
{
cout<<"nhap gia tri phan tu mang "<<i+1<<"= ";
cin>>M[i];
}
}
int toanchan(int M[5], int n)
{
for(int i=0; i<=n; i++)
{
if(M[i]%2==0)
return 1;
return 0;
}
}

61. (*)Vit chng trnh nhp s nguyn dng n gm k ch s ,(0<= k < =5) sp
xp cc ch sca n theo th t tng dn.
V d: Nhp n=1536
Kt qu sau khi sp xp: 1356.
#include <iostream.h>
#include <conio.h>
int main()
{
int a[5],so,n=0,i,j;
cout<<"Nhap 1 so toi da 5 chu so: ";
cin>>so;
do
{
a[n]=so % 10;
so=so/10;
n++;
}
while(so!=0);

//sap xep
for(i=0; i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[j]<a[i])
{
a[i]=a[i]+a[j];
a[j]=a[i]-a[j];
a[i]=a[i]-a[j];
}
so=0;
for(i=0;i<n;i++)
so=so*10+a[i];
//xuat ket qua
cout<<"So duoc viet lai theo thu tu tang:"<<endl;
cout<<so<<endl;
}

You might also like