0% found this document useful (0 votes)
16 views6 pages

Codethamkhao

The document contains multiple C programs that demonstrate various functionalities, including one-dimensional and two-dimensional array operations, file handling for storing product and student information, and basic structures for managing employee data. It includes user input for array elements, matrix addition, and conditional statements for determining properties of arrays. Additionally, there are examples of using structures to store and display information about items and students.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Codethamkhao

The document contains multiple C programs that demonstrate various functionalities, including one-dimensional and two-dimensional array operations, file handling for storing product and student information, and basic structures for managing employee data. It includes user input for array elements, matrix addition, and conditional statements for determining properties of arrays. Additionally, there are examples of using structures to store and display information about items and students.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

//===MANG 1 CHIEU

include<stdio.h>
int main(void)
{
int n,i,kt;//khai bao bien
printf("nhap so phan tu n = ");scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
printf("a[%d] = ",i+1);//nhap cac phan tu cua mang
scanf("%d",&a[i]);
}
printf("Day gom %d so nguyen da nhap la:\n",n);//in ra cac phan tu cua
mang
for(i=0;i<n;i++)
printf("\t%d",a[i]);
//y1day toan chia het cho 5
for(kt=1,i=0;i<n;i++)
if(a[i]%5!=0)//tim xem co phan tu a[i]%5!=0 -> khong phai so chia
het cho 5
{
kt=0;break;
}
if(kt)
printf("\nDay da nhap la day gom toan cac so chia het cho 5\n");
else
printf("\nDay da nhap la day k gom toan cac so chia het cho 5\
n");
//phan tu chan
printf("\nCac phan tu chan trong day da nhap la:\n");
for(dem=s=i=0;i<n;i++)
if(a[i]%2==0)
{
dem++;
printf("\ta[%d] = %d",i+1,a[i]);
s+=a[i];
}
tb=(float)s/dem;
printf("\nSo luong phan tu chan: %d\nTong cac phan tu chan: %d\nTrung
binh cac phan tu chan: %.2f\n",dem,s,tb)
//day tang
for(kt=1,i=0;i<n-1;i++)
if(a[i]>=a[i+1])
{
kt=0;break;
}
if(kt)
printf("\nDay da nhap la day tang\n");
else
printf("\nDay da nhap khong phai la day tang\n");
//doi xung
for(kt=1,i=0;i<n/2;i++)
if(a[i]!=a[n-i-1])
{
kt=0;break;
}
if(kt)
printf("\nDay da nhap la day doi xung\n");
else
printf("\nDay da nhap khong phai la day doi xung\n");
return 0;
}

//===MANG 2 CHIEU
#include<stdio.h>
int main(void)
{
int n,i,j;
printf("CHUONG TRINH TINH TONG 2 MT\nNhap kich thuoc ma tran:\nn =
");scanf("%d",&n);
int a[n][n],b[n][n],c[n][n];
printf("Nhap cac phan tu cua ma tran A:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d] = ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("Nhap cac phan tu cua ma tran B:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("b[%d][%d] = ",i+1,j+1);
scanf("%d",&b[i][j]);
}
printf("Ma tran cac so nguyen A(%d*%d) da nhap la:\n",n,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
printf("Ma tran cac so nguyen B(%d*%d) da nhap la:\n",n,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("Ma tran tong C la:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("\t%d",c[i][j]);
printf("\n");
}
return 0;
}

//=== VAO RA TEP DU LIEU


#include<stdio.h>
#include<stdio_ext.h>
#include<string.h>
int main(void)
{
FILE *fp;
int n,i;
struct mathang
{
char mathuoc[50],donvi[50],ghichu[50];
int soluong;
}hang;
printf("CHUONG TRINH TAO TEP LUU THONG TIN THONG KE BAN HANG \n");
printf("Nhap so luong mat hang thuoc: n = ");scanf("%d",&n);
fp=fopen("Thongkebanhang.dat","wb");
for(i=1;i<=n;i++)
{
printf("Mat hang thu %d:\n",i);
printf("Ma thuoc: ");fpurge(stdin);gets(hang.mathuoc);
printf("Don vi: ");gets(hang.donvi);
printf("So luong: ");scanf("%d",&hang.soluong);
if(hang.soluong>50)
strcpy(hang.ghichu,"Ban chay");
else
if(hang.soluong<5)
strcpy(hang.ghichu,"Ban cham");
else
strcpy(hang.ghichu," ");
fwrite(&hang,sizeof(hang),1,fp);
}
fclose(fp);
printf("Thong tin cac mat hang da luu trong tep la:\n");
fp=fopen("Thongkebanhang.dat","rb");
while(fread(&hang,sizeof(hang),1,fp)>0)
printf("\t%s\t%d\t%s\t%s\
n",hang.mathuoc,hang.soluong,hang.donvi,hang.ghichu);
fclose(fp);
return 0;
}

//===TRUCT
#include<stdio.h>
#include<conio.h> //<stdio_ext.h>
#include<string.h>
int main(void)
{
int n,i;
float tong;
printf("Nhap so luong mat hang: n = ");scanf("%d",&n);
struct hang
{
char tens[50];
int sl,gia,thanhtien;
}dshang[n];
for(i=0;i<n;i++)
{
printf("\nMat hang thu %d:\n",i+1);
printf("Ten mat hang: ");fflush(stdin);gets(dshang[i].tens);
//__fpurge(stdin)
printf("So luong: ");scanf("%d",&dshang[i].sl);
printf("Gia tien: ");scanf("%d",&dshang[i].gia);
dshang[i].thanhtien=dshang[i].sl*dshang[i].gia;
}
printf("\nThong tin cua %d mat hang da nhap la:\n",n);
for(i=0;i<n;i++)
printf("\t%s\t%d\t%d\t%d\
n",dshang[i].tens,dshang[i].sl,dshang[i].gia,dshang[i].thanhtien);

for(tong=0,i=0;i<n;i++)
tong+=dshang[i].thanhtien;
printf("Tong tien cua %d mat hang la: %6.2f \n",n,tong);

return 0;
}

//===De10

//Cau 2
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<fstream>
#include<iostream>
int main(void)
{
FILE *fp;
int n,i;
struct thisinh
{
char maSV[50],tenSV[50];
int diemTH;
} ts;
printf("Nhap so luong thi sinh: n = ");scanf("%d",&n);
fp=fopen("th01009.dat","wb");
for(i=1;i<=n;i++)
{
printf("Thi sinh thu %d:\n",i);
printf("Ma sinh vien: ");fflush(stdin);gets(ts.maSV);
printf("Ten sinh vien: ");gets(ts.tenSV);
printf("Diem Tin hoc dc: ");scanf("%d",&ts.diemTH);
fwrite(&ts,sizeof(ts),1,fp);
}
fclose(fp);
printf("\nthong tin cac ti sinh dc luu trong tep th01009.dat la: \n");
fp=fopen("th01009.dat","rb");
while(fread(&ts,sizeof(ts),1,fp)>0)
printf("\t%s\t%s\t%d\n",ts.maSV,ts.tenSV,ts.diemTH);
fclose(fp);
return 0;
}

//cau 1

#include<iostream>
#include<conio.h>
using namespace std;
main()
{
Y:
int thang,nam;
cout<<"Nhap thang ";cin>>thang;
cout<<"Nhap nam ";cin>>nam;
if(thang==1||thang==3||thang==5||thang==7||thang==8||thang==10||thang==12)
{
cout<<"Thang co 31 ngay";
}
if(thang==4||thang==6||thang==9||thang==11)
{
cout<<"30 ngay";
}
if(thang==2)
{
if(nam%4==0)
{
cout<<"Co 29 ngay";
}
else
{
cout<<"28 ngay";
}
}
if(thang<=0||thang>12)
{
cout<<"Nhap sai thang hay nhap lai\n";
goto Y;
}

return 0;
}

//===De14

//cau 1
#include<stdio.h>
#include<conio.h> //<stdio_ext.h>
#include<string.h>
int main(void)
{
int n,i;
printf("Nhap so luong nhan vien: n = ");scanf("%d",&n);
struct nhanvien
{
char tens[50],maNV[31],cVu[50],maPB[10];

}dsnv[n];
for(i=0;i<n;i++)
{
printf("\nThong tin nhan vien thu %d:\n",i+1);
printf("Ma nhan vien: ");fflush(stdin);gets(dsnv[i].maNV);
//__fpurge(stdin)
printf("Ten nhan vien: ");gets(dsnv[i].tens);
printf("Chuc vu: ");gets(dsnv[i].cVu);
printf("Ma phong ban: ");gets(dsnv[i].maPB);
}

printf("\nThong tin cua %d mat hang da nhap la:\n",n);


for(i=0;i<n;i++)
printf("\t%s\t%s\t%s\t%s\
n",dsnv[i].maNV,dsnv[i].tens,dsnv[i].cVu,dsnv[i].maPB);
return 0;
}

//Cau 2
#include<stdio.h>
int main(void)
{
int n,i,kt;//khai bao bien
printf("nhap so phan tu n = ");scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
printf("a[%d] = ",i+1);//nhap cac phan tu cua mang
scanf("%d",&a[i]);
}
printf("Day gom %d so nguyen da nhap la:\n",n);//in ra cac phan tu cua
mang
for(i=0;i<n;i++)
printf("\t%d",a[i]);
return 0;
}

You might also like