1. Bi 1:
Ch動董ng tr狸nh cho ph辿p nh畉p vo m畛t s畛nguy棚n d動董ng, l l動畛ng i畛n ti棚u th畛(kWh) trong
th叩ng c畛a nh b畉n. Ch動董ng tr狸nh t鱈nh v in s畛ti畛n nh b畉n ph畉i tr畉cho th叩ng 坦 動畛c t鱈nh
theo gi叩 畛nh m畛c nh動b畉ng sau:
100 kwh 畉u ti棚n 50 kwh ti畉p 50 kwh ti畉p 100 kwh ti畉p T畛300 kwh tr畛l棚n
550 VND 900 VND 1250 VND 1450 VND 1700 VND
v ph畉i 坦ng thu畉gi叩 tr畛gia tng 10%
Bi 2:
Nh畉p vo m畛t s畛nguy棚n c坦 2 ch畛s畛, h達y in ra c叩ch 畛c c畛a n坦.
Bi 3:
Nh畉p vo m畛t s畛nguy棚n c坦 3 ch畛s畛, h達y in ra c叩ch 畛c c畛a n坦.
nt N; //S畛c坦 3 ch畛s畛c畉n 畛c
int tram, chuc, donvi;
tram = N / 100;
donvi = N % 10;
chuc = (N tram * 100) / 10;
Bi 4:
Vi畉t ch動董ng tr狸nh t鱈nh n!! v畛i n!! = 1.3.5n n畉u n l畉, n!! = 2.4.6n n畉u n ch畉n.
HD:
Ta c畉n x叩c 畛nh i ch畉y t畛1 hay 2 ph畛thu畛c vo n ch畉n hay l畉?
for(int i = (n % 2)? 1 : 2; i <= n; i += 2) gt *= i;
Bi 5:
Vi畉t ch動董ng tr狸nh nh畉p vo s畛nguy棚n n. T鱈nh v in ra c叩c t畛ng sau:
Bi 6:
Vi畉t ch動董ng tr狸nh nh畉p vo m畛t s畛nguy棚n d動董ng, ki畛m tra 坦 c坦 ph畉i l s畛nguy棚n t畛hay
kh担ng?
2. Bi 7:
Vi畉t ch動董ng tr狸nh nh畉p vo m畛t s畛nguy棚n d動董ng, xu畉t ra d畉ng ph但n t鱈ch th畛a s畛nguy棚n
t畛c畛a s畛 坦.
HD: int n;
cout<<Nhap n: ;
cin>>n;
cout<<n<<" = ";
int i= 2;
while(i <= n)
{
if(n % i == 0){
cout<<i;
n /= i;
if(n != 1) cout<<"*";
}
else i++;
}
Bi 8:
Vi畉t ch動董ng tr狸nh t狸m 動畛c chung l畛n nh畉t, b畛i chung nh畛nh畉t c畛a 2 s畛nguy棚n M, N nh畉p
t畛bn ph鱈m.
Bi 9:
Vi畉t ch動董ng tr狸nh nh畉n 1 gi叩 tr畛nguy棚n di v cho hi畛n th畛ra mn h狸nh s畛 坦 d畉ng: h畛10,
h畛16, h畛8, h畛2.
Bi 10:
Bi t畉p 44: (*)
Vi畉t ch動董ng tr狸nh t鱈nh sin(x) theo c担ng th畛c x畉p x畛:
v畛i 畛 ch鱈nh x叩c 0.00001. T畛c l t鱈nh cho t畛i n
3. sao cho: 00001 .0
//Chuong trinh nhap mot so nguyen va in ket qua co phai la so chinh phuong hay khong
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n,s;
float c;
printf("Ban hay nhap mot so nguyen: n"); scanf("%d",&n);
if (n<=0) printf("So ",n," khong phai so chinh phuong n"); else
{
for (i=1;i<=n;i++)
{
if(i*i==n) s=1;
}
if(s==1) printf("so ban nhap la so chinh phuong");
else printf("so ban nhap khong la so chinh phuong");
}
getch();
}
Bi 11: Vi畉t ch動董ng tr狸nh nh畉p vo m畛ts畛nguy棚n d動董ng,xu畉t ra d畉ng ph但n t鱈ch th畛a s畛nguy棚n
HD:
int n;
cout<<Nhap n: ;
cin>>n;
cout<<n<<" = ";
int i= 2;
while(i <= n)
{
if(n % i == 0){
cout<<i;
n /= i;
if(n != 1) cout<<"*";
}
else i++;
}
4. #include<conio.h>
#include<stdio.h>
#include<malloc.h>
int main()
{
int i=0,coso=0,n=0,du=0,*mang;
mang=(int*)malloc(100*sizeof(int));
printf("Nhap vao mot con so: ");scanf("%d",&n);
printf("Nhap vao co so muon chuyen doi: ");scanf("%d",&coso);
int m=n;
do
{
du=n%coso;
n/=coso;
mang[i]=du;
i++;
}while(n>0);
if(coso==16)
{
printf("So %d sau khi chuyen sang co so %d: n",m,coso);
for(int j=i-1;j>=0;j--)
{
switch(mang[j])
{
case 10: printf("A");
break;
case 11: printf("B");
break;
case 12: printf("C");
break;
case 13: printf("D");
break;
case 14: printf("E");
break;
case 15: printf("F");
break;
default: printf("%d",mang[j]);
}
}
}
else
{
printf("So %d sau khi chuyen sang co so %d: n",m,coso);
for(int j=i-1;j>=0;j--)
{
printf("%d",mang[j]);
5. }
}
getch();
return 0;
}
#include <stdio.h>
#include <conio.h>
void chuyendoi(int so,int he)
{
if (so>=he) chuyendoi(so/he,he);
printf("%c",(so%he)["0123456789ABCDEF"]);
}
void main(void)
{
int n,a;
printf("Nhap vao so : ");
scanf("%d",&n);
printf("Chuyen sang he co so : ");
scanf("%d",&a);
chuyendoi(n,a);
getch();
}
#include "stdio.h"
#include "conio.h"
main()
{
int i=0,j,n,m,du,mang[20];
clrscr();
printf("Nhap so n: ");
scanf("%d",&n);
m = n;
do
{
du = m%2;
mang[i] = du;
m=m/2;
i++;
} while(m>0);
printf("nSo %d o dang co so 2 la: ",n);
for (j=i;j>=0;j--)
printf("%d",mang[j]);
printf("nSo %d o dang co so 8 la: %o",n,n);
printf("nSo %d o dang co so 16 la: %x",n,n);
getch();
return 0;
}
6. Vi畉t ch動董ng tr狸nh t鱈nh
V畛i x v 畛 ch鱈nh x叩c Eps th畛c nh畉p t畛 bn ph鱈m, t畛c l t鱈nh cho 畉n khi no
abs((x^(2n+1))/((2n+1)!)) <= Eps th狸 d畛ng. In k畉t qu畉 ra mn h狸nh.
#include "stdio.h"
#include "conio.h"
#include "math.h"
main()
{
float Eps,eps,sinx,x,ra;
int n,i,j,gt;
clrscr();
printf("Nhap x: ");
scanf("%f",&x);
ra = x*3.14159/180;
printf("Nhap phan chinh xac Eps: ");
scanf("%f",&eps);
sinx=0;Eps=0;n=0;
do
{
gt=1;
j=2*n+1;
for (i=1;i<=j;i++)
gt=gt*i;
Eps = pow(ra,j)/gt;
if (n%2==0)
sinx = sinx + Eps;
else sinx = sinx - Eps;
n++;
//printf("ngt: %d,x: %f",gt,x);
//printf("nEps: %f",Eps);
} while (Eps>eps);
printf("Ket qua: %f",sinx);
getch();
return 0;
}
T狸m gi叩 tr畛 l畛n nh畉t v 畉m c叩c gi叩 tr畛 l畛n nh畉t c畛a n畛a d動畛i 動畛ng ch辿o ph畛 c畛a m畛t
ma tr畉n c畉p A[n,n]. In ma tr畉n c湛ng k畉t qu畉 t狸m 動畛c ra mn h狸nh.
#include "stdio.h"
#include "conio.h"
main()
{
int i,j, n,t;
float a[100][100],max;
clrscr();
printf("Nhap so n: ");
scanf("%d",&n);
if (n>0)
{
7. for (i=0;i<n;i++)
for (j=0;j<n;j++)
{
printf("Nhap gia tri a[%d][%d]: ",i,j);
scanf("%f",&a[i][j]);
}
max = a[2][n-1];
for (i=1;i<n;i++)
for (j=n-i;j<n;j++)
if (max < a[i][j]) max = a[i][j];
t=0;
for (i=1;i<n;i++)
for (j=n-i;j<n;j++)
if (a[i][j] == max) t=t+1;
printf("nMa tran A:"); printf("n");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
printf("%6.2f",a[i][j]);
printf("n");
}
printf("nGia tri lon nhat la %6.2f",max);
printf("nTim duoc %d vi tri.",t);
}
else printf("Ban phai nhap so nguyen duong!");
getch();
return 0;
}
T鱈nh t鱈ch hai ma tr畉n C[n,p] = A[n,m] x B[m,p]. In c叩c ma tr畉n ra mn h狸nh.
#include "stdio.h"
#include "conio.h"
main()
{
int i,j,t,n1,n2,m1,m2;
float a[100][100],b[100][100],c[100][100];
clrscr();
printf("nNhap cap cua ma tran a: ");
scanf("%d%d",&n1,&n2);
printf("nNhap cap cua ma tran b: ");
scanf("%d%d",&m1,&m2);
if (n2!=m1) printf("Hai ma tran khong nhan duoc!");
else if (n1>0&&n2>0&&m2>0)
{
for (i=0;i<n1;i++)
for (j=0;j<n2;j++)
{
printf("Nhap gia tri a[%d][%d]: ",i,j);
scanf("%f",&a[i][j]);
}
for (i=0;i<n2;i++)
for (j=0;j<m2;j++)
{
printf("Nhap gia tri b[%d][%d]: ",i,j);
scanf("%f",&b[i][j]);
}
for (i=0;i<n1;i++)
8. for (j=0;j<m2;j++)
for (t=0;t<n2;t++)
c[i][j] = c[i][j] + (a[i][t])*(b[t][j]) ;
printf("Ma tran a: n");
for (i=0;i<n1;i++)
{
for (j=0;j<n2;j++)
printf("%6.2f",a[i][j]);
printf("n");
}
printf("Ma tran b: n");
for (i=0;i<m1;i++)
{
for (j=0;j<m2;j++)
printf("%6.2f",b[i][j]);
printf("n");
}
printf("Ma tran c: n");
for (i=0;i<n1;i++)
{
for (j=0;j<m2;j++)
printf("%6.2f",c[i][j]);
printf("n");
}
}
else printf("Ban phai nhap so nguyen duong!");
getch();
return 0;
}
Vi畉t ch動董ng tr狸nh nh畉p vo s畛 nguy棚n d動董ng n. Sau 坦 in ra mn h狸nh ma tr畉n xo叩y
A[n,n] theo d畉ng:
123
894
7 6 5 v畛i n = 3
#include "stdio.h"
#include "conio.h"
main()
{
int i,j,n,v,m, A[100][100];
clrscr();
printf("Nhap so chieu n cua ma tran vuong a: ");
scanf("%d",&n);
if (n%2==0) m=n/2;
else { m=n/2+1; A[n/2][n/2]=n*n;}
v=1;
for (i=0;i<m;i++)
{
for (j=i;j<n-i-1;j++) { A[i][j] = v; ++v; }
for (j=i;j<n-i-1;j++) { A[j][n-i-1] = v; ++v; }
for (j=n-i-1;j>i;j--) { A[n-i-1][j] = v; ++v; }
for (j=n-i-1;j>i;j--) { A[j][i] = v; ++v; }
}
for (i=0;i<n;i++)
{
9. for (j=0;j<n;j++) printf("%6d",A[i][j]);
printf("n");
}
getch();
return 0;
}
Vi畉t ch動董ng tr狸nh nh畉p vo hai d達y s畛 nguy棚n theo chi畛u tng d畉n. Sau 坦 tr畛n hai d達y
ny l畉i thnh d達y th畛 ba v畉n theo chi畛u tng d畉n m kh担ng c畉n s畉p x畉p l畉i. In ra mn
h狸nh ba d達y.
#include "stdio.h"
#include "conio.h"
main()
{
int i,j,t,n,m,p,a[100],b[100],c[100];
clrscr();
printf("nSo phan tu cua day thu nhat: ");
scanf("%d",&n);
printf("nSo phan tu cua day thu hai: ");
scanf("%d",&m);
if (n>0&&m>0)
{
for (i=0;i<n;i++)
{
printf("nNhap phan tu thu %d cua day thu nhat: ",i+1);
scanf("%d",&a[i]);
}
for (i=0;i<m;i++)
{
printf("nNhap phan tu thu %d cua day thu hai: ",i+1);
scanf("%d",&b[i]);
}
p = m + n;
t=j=0;
for (i=0;i<p;i++)
{
if (b[j] > a[t])
{
c[i] = a[t];
++t;
if (t==n) { c[i+1] = b[j]; break; }
}
else
{
c[i] = b[j];
++j;
if (j==m) { c[i+1] = a[t]; break; }
}
}
printf("nDay thu nhat: ");
for (i=0;i<n;i++)
printf("%4d",a[i]);
printf("nDay thu hai: ");
for (i=0;i<m;i++)
printf("%4d",b[i]);
printf("nDay thu ba: ");
10. for (i=0;i<p;i++)
printf("%4d",c[i]);
}
else printf("Ban phai nhap so nguyen duong!");
getch();
return 0;
}
Bi 4: Vi畉t ch動董ng tr狸nh t鱈nh t畛ng hai ma tr畉n C(nxm) = A(nxm) + B(nxm), trong 坦 c坦
vi畉t hm nh畉p ma tr畉n, hm in ma tr畉n. In c叩c ma tr畉n ra mn h狸nh.
#include "stdio.h"
#include "conio.h"
void nhap(float (*a)[100],int hang,int cot,char ch)
{
int i,j;
for (i=0;i<hang;i++)
for (j=0;j<cot;j++)
{
printf("Nhap phan tu %c[%d][%d]: ",ch,i,j);
scanf("%f",&a[i][j]);
}
}
void in(float (*a)[100],int hang,int cot)
{
int i,j;
for (i=0;i<hang;i++)
{
for (j=0;j<cot;j++)
printf("%5.2f",a[i][j]);
printf("n");
}
}
void cong(float (*a)[100],float (*b)[100],float (*c)[100],int hang,int cot)
{
int i,j;
for (i=0;i<hang;i++)
for (j=0;j<cot;j++)
c[i][j] = a[i][j] + b[i][j];
}
int main()
{
float a[100][100],b[100][100],c[100][100];
int hang,cot;
clrscr();
printf("Nhap so hang, so cot cua hai ma tran: ");
scanf("%d%d",&hang,&cot);
nhap(a,hang,cot,'A');
nhap(b,hang,cot,'B');
cong(a,b,c,hang,cot);
printf("nMa tran A: n");
in(a,hang,cot);
printf("nMa tran B: n");
in(b,hang,cot);
printf("nMa tran C: n");
11. in(c,hang,cot);
getch();
return 0;
}
Bi 5: Vi畉t ch動董ng tr狸nh t鱈nh t鱈ch hai ma tr畉n C(nxm) = A(nxp) + B(pxm), trong 坦 c坦 vi畉t
hm nh畉p ma tr畉n, hm in ma tr畉n. In c叩c ma tr畉n ra mn h狸nh.
#include "stdio.h"
#include "conio.h"
void nhap(float (*a)[100],int hang,int cot,char ch)
{
int i,j;
for (i=0;i<hang;i++)
for (j=0;j<cot;j++)
{
printf("nNhap phan tu %c[%d][%d]: ",ch,i,j);
scanf("%f",&a[i][j]);
}
}
void in(float (*a)[100],int hang,int cot)
{
int i,j;
for (i=0;i<hang;i++)
{
for (j=0;j<cot;j++)
printf("%6.2f",a[i][j]);
printf("n");
}
}
void nhan(float (*a)[100],float (*b)[100],float (*c)[100],int hang1,int cot1,int
cot2)
{
int i,j,t;
for (i=0;i<hang1;i++)
for (j=0;j<cot2;j++)
for (t=0;t<cot1;t++)
c[i][j] = c[i][j]+a[i][t]*b[t][j];
}
int main()
{
float a[100][100],b[100][100],c[100][100];
int hang1,cot1,hang2,cot2;
clrscr();
printf("nNhap so hang va so cot cua ma tran A: ");
scanf("%d%d",&hang1,&cot1);
printf("nNhap so hang va so cot cua ma tran B: ");
scanf("%d%d",&hang2,&cot2);
if (cot1!=hang2)
{
printf("nHai ma tran khong the nhan duoc!");
return 0;
}
nhap(a,hang1,cot1,'A');
nhap(b,hang2,cot2,'B');
12. nhan(a,b,c,hang1,cot1,cot2);
printf("nMa tran A: n");
in(a,hang1,cot1);
printf("nMa tran B: n");
in(b,hang2,cot2);
printf("nMa tran C: n");
in(c,hang1,cot2);
getch();
return 0;
}
Bi 7: Vi畉t hm t狸m 動畛c chung l畛n nh畉t c畛a hai s畛 nguy棚n d動董ng theo ph動董ng ph叩p 畛
quy.
#include <stdio.h>
#include "conio.h"
int UCLN(int a,int b)
{
int m,n;
m=a;n=b;
if (m>n) m=m-n;
else if (n>m) n=n-m;
if (m!=n) m=UCLN(m,n);
return m;
}
int main()
{
int a,b;
clrscr();
printf("nNhap hai so : ");
scanf("%d%d",&a,&b);
printf("nUCLN cua hai so %d va %d la: %d",a,b,UCLN(a,b));
getch();
return 0;
}
pro no gi炭p m狸nh v畛i
vBi 1: 畉m xem c坦 bao nhi棚u t畛 trong s. Xu畉t c叩c t畛 tr棚n c叩c d嘆ng li棚n ti畉p.
vBi 2: T狸m t畛 c坦 chi畛u di di nh畉t v in ra.
vBi 3: Vi畉t hm upper 畛i ton b畛 c叩c k箪 t畛 sang k箪 t畛 hoa (gi畛ng hm strupr)
vBi 4: Vi畉t hm lower 畛i ton b畛 c叩c k箪 t畛 sang k箪 t畛 th動畛ng (gi畛ng hm strlwr)
vBi 5: Vi畉t hm proper 畛i c叩c k箪 t畛 畉u ti棚n c畛a m畛i t畛 sang k箪 t畛 hoa.
vBi 6: Vi畉t hm ltrim c畉t b畛 c叩c kho畉ng tr畉ng b棚n tr叩i chu畛i.
vBi 7: Vi畉t hm rtrim c畉t b畛 c叩c kho畉ng tr畉ng b棚n ph畉i chu畛i.
vBi 8: Vi畉t hm trim b畛 ton b畛 kho畉ng tr畉ng 畉u chu畛i, cu畛i chu畛i v gi畛a 2 t畛 trong s
ch畛 c嘆n 1 kho畉ng tr畉ng.
vBi 9: Vi畉t hm del_char x坦a c叩c k箪 t畛 ch畛 畛nh.
vBi 9a: Vi畉t hm del_char x坦a c叩c k箪 t畛 ch畛 畛nh t畛 m畛t v畛 tr鱈 ch畛 畛nh.
vBi 10: Vi畉t hm char *ins_s(char *subs, char *s, int pos) ch竪n th棚m subs vo s t畉i v畛 tr鱈
ch畛 畛nh pos.
vBi 11: Vi畉t hm char *del_s(char *s, int pos, int len) x坦a len k箪 t畛 trong chu畛i s t畛 v畛 tr鱈
ch畛 畛nh pos.
13. vBi 12: Vi畉t hm char *left(char *s, int len): tr鱈ch chu畛i con t畛 chu畛i s (t畛 b棚n tr叩i sang
di len k箪 t畛)
vBi 13: Vi畉t hm char *right(char *s, int len).
vBi 14: Vi畉t hm char *sub(char *s, int pos, int len): tr鱈ch chu畛i con t畛 chu畛i s (t畛 v畛 tr鱈
pos di len k箪 t畛)
vBi 15: Vi畉t hm char *revsub(char *s, int pos, int len): t動董ng t畛 sub nh動ng tr鱈ch t畛
ph畉i sang tr叩i.
Bi 9:
void Strdel(char s[], int n)
{
for (int i = n; i < Strlen(s) - 1; i++)
{
s[i] = s[i + 1];
}
s[Strlen(s)-1] = '0';
}
void Strchdel(char s[])
{
int i = 0;
printf("Choose character to delete: ");
char c;
scanf("%c", &c);
while (i < Strlen(s))
{
if (s[i] == c)
{
Strdel(s, i);
}
else
{
i++;
}
}
}
Xu畉t ra h畛 v t棚n
#include<iostream.h>
#include<conio.h>
#include<string.h>
14. //using namespace std;
void main()
{
char str[30];
cout<<" Xau : ";gets(str);
int n=strlen(str);
int i,k,k1;
for(i=n-1;i>=0;i--)
{
if(str[i]==' ')
{
k=i;break;
}
}
for(i=0;i<=n-1;i++)
{
if(str[i]==' ')
{
k1=i;break;
}
}
cout<<"n TEN :";
for(i=k+1;i<=n-1;i++)
{
cout<<str[i];
}
cout<<"n TEN DEM :";
for(i=k1+1;i<k;i++)
{
cout<<str[i];
}
cout<<"n HO :";
for(i=0;i<k1;i++)
{
cout<<str[i];
}
getch();
}
T狸m ki畉m ph畉n t畛x tr棚n danh s叩ch theo c叩c ti棚u ch鱈 sau:
o M畛t s畛x cho tr動畛c,
o S畛l畛n nh畉t,
o S畛b辿 nh畉t,
o S畛nguy棚n t畛d畉u ti棚n,
o S畛ch鱈nh ph動董ng 畉u ti棚n,
o S畛nguy棚n t畛l畛n nh畉t,