0% found this document useful (0 votes)
42 views

1.Tổng hai ma trận: #include #include #include

The document contains 17 code snippets in Vietnamese that demonstrate various programming concepts and techniques including: 1. Summing two matrices 2. Multiplying two matrices 3. Finding all perfect numbers below a given number N 4. Printing the lunar year corresponding to a given solar year 5. Calculating the day of the week for a given date The code snippets cover topics such as arrays, loops, functions, strings, matrices, dates, prime numbers, factorials, Fibonacci numbers, and more. Various algorithms and data structures are implemented to solve problems related to number theory, date calculations, string manipulation and more.

Uploaded by

Xuan Minh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

1.Tổng hai ma trận: #include #include #include

The document contains 17 code snippets in Vietnamese that demonstrate various programming concepts and techniques including: 1. Summing two matrices 2. Multiplying two matrices 3. Finding all perfect numbers below a given number N 4. Printing the lunar year corresponding to a given solar year 5. Calculating the day of the week for a given date The code snippets cover topics such as arrays, loops, functions, strings, matrices, dates, prime numbers, factorials, Fibonacci numbers, and more. Various algorithms and data structures are implemented to solve problems related to number theory, date calculations, string manipulation and more.

Uploaded by

Xuan Minh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

Tng hai ma trn


#include <iostream.h> #include <conio.h> #include <stdlib.h> void congmt(float a[][10],float b[][10],float c[][10],int hang,int cot); void nhapmt(float a[][10],int hang,int cot); void inmt(float a[][10],int hang,int cot); void main() { system("color 3e"); float a[10][10],b[10][10],c[10][10]; int hang1,cot1; cout<<"Moi ban nhap vao ma tran a: \n"; cout<<"Nhap vao so hang cua ma tran a: "; cin>>hang1; cout<<"Nhap vao so cot cua ma tran a: "; cin>>cot1; nhapmt(a,hang1,cot1); inmt(a,hang1,cot1); int hang2,cot2; cout<<"Moi ban nhap vao ma tran b: \n"; do { cout<<"Nhap vao so hang cua ma tran b: "; cin>>hang2; }while(hang2 != hang1); do { cout<<"Nhap vao so cot cua ma tran b: "; cin>>cot2; }while(cot2 != cot1); nhapmt(b,hang2,cot2); inmt(b,hang2,cot2); cout<<"\nVay tong cua hai ma tran a,b la: \n"; congmt(a,b,c,hang1,cot1); inmt(c,hang1,cot1); getch();

void congmt(float a[][10],float b[][10],float c[][10],int hang,int cot) { for (int i=0; i<hang; i++) for (int j=0; j<cot; j++) c[i][j] = a[i][j] + b[i][j]; } void nhapmt(float a[][10],int hang,int cot) { for(int i = 0;i < hang;i++) { for(int j = 0; j < cot; j++) { cout<<"Nhap vao phan tu ["<<i<<";"<<j<<"]: "; cin>>a[i][j]; } } }

void inmt(float a[][10],int hang,int cot) { for(int i = 0; i < hang; i++) { for(int j = 0; j < cot; j++) { cout<<a[i][j]<<"\t"; } cout<<endl; } }

2.Tnh tch 2 ma trn

#include<conio.h> #include<stdio.h> #include<alloc.h> void main() { int *a,*b,*c; int m,n; int i,j; clrscr(); //Nhap so hang so cot printf("Nhap vao m:");scanf("%d",&m); printf("Nhap vao n:");scanf("%d",&n); //Cap phat bo nho a=(int*)calloc(m*n,sizeof(int)); b=(int*)calloc(m*n,sizeof(int)); c=(int*)calloc(m*n,sizeof(int)); // Nhap so lieu va tinh toan for(i=1;i<=m;i++) for(j=1;j<=n;j++) { printf("Nhap vao a[%d,%d]=",i,j);scanf("%d",&a[(i-1+j)+((i-1)*(n1))]); } for(i=1;i<=m;i++) for(j=1;j<=n;j++) { printf("Nhap vao b[%d,%d]=",i,j);scanf("%d",&b[(i-1+j)+((i-1)*(n1))]); c[(i-1+j)+((i-1)*(n-1))]=a[(i-1+j)+((i-1)*(n-1))]+b[(i-1+j)+((i1)*(n-1))]; } // xuat cac mang a,b,c ra man hinh for(i=1;i<=m;i++) for(j=1;j<=n;j++) { printf("\t%d",a[(i-1+j)+((i-1)*(n-1))]); if(j==n)printf("\n"); } printf("\n===========\n"); for(i=1;i<=m;i++) for(j=1;j<=n;j++) { printf("\t%d",b[(i-1+j)+((i-1)*(n-1))]); if(j==n)printf("\n"); } printf("\n===========\n"); for(i=1;i<=m;i++) for(j=1;j<=n;j++) {

printf("\t%d",c[(i-1+j)+((i-1)*(n-1))]); if(j==n)printf("\n"); } getch(); }

3.In danh sch cc s hon ho nh hn s N nhp t bn phm


#include <stdio.h> int main(void) { int i , j , n , tong ; printf("\n Nhp vo N = "); scanf("%d",&n); for ( i = 2 ; i <=n ; i++ ) { tong = 1 ; for ( j = 2; j <= i / 2 ; j++ ) if ( i % j == 0 ) tong += j; if ( tong == i ) printf(" %10d ", i ); } getch(); return 0; }

4.In ra nm m lch tng ng vi nm nhp vo.


#include <stdio.h> #include <conio.h> void main() { unsigned nam; char can[][5] = {"Giap", "At", "Binh", "Dinh", "Mau", "Ky", "Canh", "Tan", "Nham", "Quy"}; char chi[][5] = {"Ty", "Suu", "Dan", "Meo", "Thin", "Ty", "Ngo", "Mao", "Than", "Dau", "Tuat", "Hoi"}; printf("\nNhap nam can biet : "); scanf("%d", &nam); printf("Nam am lich cua %d la %s %s", nam, can[(nam+6)%10], chi[(nam+8)%12]); getch(); }

5.Tnh th ca ngy
#include <stdio.h> #include <conio.h> struct date { int month; int day; int year; } date_1; long int funct1 (int y,int m) { long int result;

if ( m y -= result return }

<= 2 ) 1; = y; (result);

long int funct2 (int m) { long int result; if ( m <= 2 ) result = m + 13; else result = m + 1; return(result); } long int day_count (int m, int d, int y) { long int number; number = 1461 * funct1(y,m) / 4 + 153 * funct2(m) / 5 + d; return (number); } void main () { long int number_of_days1; int day_of_week; printf ("Nhap vao mot ngay (dd mm yyyy), vd 12 03 1999 \n"); scanf ("%d %d %d", &date_1.day, &date_1.month, &date_1.year); number_of_days1 = day_count (date_1.month, date_1.day, date_1.year); printf ("\nNgay la : " ); day_of_week = (number_of_days1 - 621049) % 7; switch (day_of_week) { case 0 : printf ("Chu Nhat,"); break; case 1 : printf ("Thu Hai,"); break; case 2 : printf ("Thu Ba,"); break; case 3 : printf ("Thu Tu,"); break; case 4 : printf ("Thu Nam,"); break; case 5 : printf ("Thu Sau,"); break; case 6 : printf ("Thu Bay,"); break; }

getch();

6.Biu din s di dng bit


#include <stdio.h> #include <conio.h> void main() { unsigned int mang[24], i; int bit[16], k, index; printf("\nNhap vao 23 gia tri nguyen : "); for (i=0; i<23; i++) scanf("%d",&mang[i]); printf(" FEDCBA9876543210"); for (i=0; i<23; i++) { k = mang[i]; for (index = 0; index < 16; index++) bit[index] = 0; index = 15; while (k) { bit[index--] = k%2; k /= 2; } printf("\n%5d ",mang[i]); for (index=0; index<16; index++) if (bit[index] == 1) printf("*"); else printf("-"); } getch()

7.o chui nhp vo t bn phm.


char *dnchuoi(char *s) { char *tmp, i; i = 0; tmp = (char *)malloc(strlen(s)+1); while (i<strlen(s)) *(tmp+i) = *(s + strlen(s) - i++ - 1); *(tmp+i) = 0; return tmp;

void main() { char hello[] = "Hello World"; char *s; printf("\nChuoi ban dau = %s", hello); s = dnchuoi(hello); printf("\nChuoi dao nguoc = %s", s); getch(); }

8.In chui theo cc t mi t mt dng.


#include <stdio.h> #include <conio.h> #include <string.h> void main() { char s[50]; int i, len; printf("\nNhap vao mot chuoi : "); gets(s); len = strlen(s); i = 0; while (i<len) { while (s[i] == ' ' && i<len) i++; while (s[i] != ' ' && i<len) putc(s[i++], stdout); putc('\n', stdout); } getch();

9.In ra ch s hng trm hng chc hng n v


#include <stdio.h> #include <conio.h> void main() { int n, tram, chuc, donvi; clrscr(); printf("\nNhap vao mot so tu 100 - 999 : "); scanf("%d", &n); tram = n; donvi = tram % 10; tram /= 10; chuc = tram % 10; tram /= 10; printf("\nSo hang tram = %d", tram); printf("\nSo hang chuc = %d", chuc); printf("\nSo hang don vi = %d", donvi); } getch();

10.Tm phn t ln nht nh nht trong mng mt chiu.


#include <conio.h> #include <stdlib.h> void main() { int mang[20];

int i, minval, maxval; /* Khoi tao mang ngau nhien */ randomize(); for (i=0; i<20; i++) mang[i] = random(100); /* Tim gia tri lon nhat va nho nhat */ minval = maxval = mang[0]; for (i=1; i<20; i++) { if (maxval < mang[i]) maxval = mang[i]; else if (minval > mang[i]) minval = mang[i]; } /* In mang */ clrscr(); for (i=0; i<20; i++) { if (mang[i] == maxval) textcolor(YELLOW); else if (mang[i] == minval) textcolor(RED); else textcolor(WHITE); cprintf("%3d", mang[i]); } getch(); }

11.Tnh t hp chp K ca N phn t.


#include <stdio.h> #include <conio.h> unsigned long giaithua(int n) { unsigned long ketqua = 1; int i; for (i=2; i<=n; i++) ketqua *= i; } return ketqua;

unsigned long to_hop_chap(int k, int n) { return giaithua(n) / (giaithua(k) * giaithua(n-k)); } void main() { int n, k;

printf("\nNhap vao gia tri N va K : "); scanf("%d%d", &n, &k); printf("Top hop chap %d cua %d = %lu", k, n, to_hop_chap(k, n)); getch();

12.Tnh s ngy trong mt thng trong mt nm bt k.


/* Day la chuong trinh tinh so ngay trong bat ky mot thang trong nam bat ky nao do Duoc viet boi Vu Thanh Nam */ #include<iostream.h> #include<conio.h> #include<math.h> void main() { long int thang, nam; cout<<"Nhap thang va nam: "; cin>>thang>>nam; if ((thang>12||thang<1)||(nam<0)) cout<<"Nhap thang hoac nam sai"; else { bool namnhuan=((nam%4==0&&nam%100!=0)||(nam%400==0&&nam%100==0)); int songay; if(thang==4||thang==6||thang==9||thang==11) songay = 30; else { if(thang==2) songay = namnhuan?29:28; else songay=31; } cout<<"So ngay cua thang "<<thang<<" trong nam "<<nam<<" la: "<<songay<<endl; } getch(); }

13.Bi kim tra s nguyn t.


/*Ham tinh so nguyen to */ #include<iostream.h> #include<conio.h> #include<math.h> bool LaNguyenTo(int n); void main() { int n; cout<<"Nhap vao mot so bat ky: ";cin>>n; if(LaNguyenTo(n)) cout<<"La so nguyen to"; else { cout<<"Khong la so nguyen to vi no chia het cho "; for(int j=2;j<n;j++) { if(n%j==0)

} bool LaNguyenTo(int n) { bool co=true; if(n<2) {cout<<"Du lieu nhap sai va so do "; return !co;} else { for(int i=2;i<=sqrt(n)+1;i++) { if(n%i==0) { co=false; break; } } } return co; }

} getch();

cout<<j<<" va "; } cout<<"The thoi";

14.Tm max min ca 4 s


#include<iostream.h> #include<conio.h> #include<math.h> float max(int a, int b); float min(int a, int b); void main() { int a,b,c,d; cout<<"Nhap vao so thu 1: ";cin>>a; cout<<"Nhap vao so thu 2: ";cin>>b; cout<<"Nhap vao so thu 3: ";cin>>c; cout<<"Nhap vao so thu 4: ";cin>>d; cout<<"max= "<<max(max(max(a,b),max(b,c)),max(c,d))<<" min= "<<min(min(min(a,b),min(b,c)),min(c,d))<<endl; cout<<"Vay trung binh cong cua 4 so do la: "<<(a+b+c+d)/4; getch(); } float max(int a, int b) { if(a>b) return a; else return b; } float min(int a, int b) { if(a>b) return b; else return a; }

15.Tm n s Fibonaci u tin


#include<iostream.h> #include<conio.h> #include<math.h> long Fibonaci(long n); void main() { long n; cout<<"Nhap vao mot so nguyen duong: "; cin>>n; cout<<"Vay "<<n<< " so Fibonaci dau tien la: "; Fibonaci(n); getch(); } long Fibonaci(long n) //sach giao khoa 253 { int f1=0,f2=1,fi; fi=f2; for(int i = 1;i <= n ;i ++) { cout<<fi<<" "; fi=f1+f2; f1=f2; f2=fi; } return fi; }

16.Loi b khong trng tha trong chui


#include <stdio.h> #include <string.h> #include <conio.h> #pragma warn -pia char *trim(char *chuoi) { char *p; while (p = strstr(chuoi, " ")) memmove(p, p+1, strlen(chuoi) - (p - chuoi)); if (chuoi[0] == ' ') memmove(chuoi, chuoi+1, strlen(chuoi) - 1); if (chuoi[strlen(chuoi)-1] == ' ') chuoi[strlen(chuoi)-1] = 0; return chuoi; } void main() { char chuoi[125]; printf("\nNhap chuoi mau : "); textattr(0x1e); gets(chuoi); trim(chuoi); printf("\nChuoi sau khi da trim : "); textattr(0x1e); cputs(chuoi); getch(); }

17.Tim tt ca cac c cua mt s N


#include <stdio.h> #include <conio.h> void main() { int n, i; printf("Cho gia tri N = "); scanf("%d", &n); printf("Cac uoc so cua %d la :\n", n); for (i=1; i<n; i++) if ((n % i) == 0) printf("%5d", i); getch();

18.Bi s chung v c s chung ca 2 s


#include <stdio.h> #include <conio.h> unsigned USCLN (unsigned n, unsigned m) { while (n != 0 && m != 0) if (n>m) n -= m; else m -= n; if (n == 0) return m; else return n; } unsigned BSCNN (unsigned n, unsigned m) { return n * m / USCLN(n, m); } void main() { unsigned n, m; printf("\nNhap hai vao so nguyen duong : "); scanf("%u%u", &n, &m); printf("\nUSCLN cua %u va %u = %u", n, m, USCLN(n,m)); printf("\nBSCNN cua %u va %u = %u", n, m, BSCNN(n,m)); getch();

19.Trn 2 dy gim thnh mt dy tng


#include <stdio.h> #include <conio.h> #define MAX 10 void main() { int a[MAX], b[MAX], c[2*MAX], n1, n2, i, i1, i2; printf("\nCho biet so phan tu cua mang thu nhat : "); scanf("%d", &n1); printf("Nhap vao cac phan tu (giam dan) cua mang thu nhat : "); for (i=0; i<n1; i++)

scanf("%d", &a[i]); printf("\nCho biet so phan tu cua mang thu hai : "); scanf("%d", &n2); printf("Nhap vao cac phan tu (giam dan) cua mang thu hai : "); for (i=0; i<n2; i++) scanf("%d", &b[i]); i1 = n1-1; i2 = n2-1; for (i=0; i<n1 + n2; i++) { if (i1 < 0 || i2 < 0) break; if (a[i1] < b[i2]) { c[i] = a[i1]; i1--; } else { c[i] = b[i2]; i2--; } } if (i1 >= 0) while (i1 >= 0) c[i++] = a[i1--]; if (i2 >= 0) while (i2 >= 0) c[i++] = b[i2--]; printf("\nCac phan tu cua mang tron : "); for (i=0; i<n1+n2; i++) printf("%d ", c[i]); getch();

20.Kim tra s i xng


/*Vu Thanh Nam Bai tap 43 */ #include<iostream.h> #include<conio.h> #include<math.h> long songuoc(long a); void main() { long n; do { cout<<"Nhap vao mot so bat ky: "; cin>>n; }while(n<0); if(n==songuoc(n)) cout<<"La so doi xung"; else cout<<"Khong la so doi xung"; getch(); } long songuoc(long a) { int tg; if (a > 0) {

} return tg;

while (a > 0) { tg= a%10; a = a / 10; }

You might also like