0% found this document useful (0 votes)
5 views9 pages

Bài thực hành 7

The document contains a series of C programming exercises, each demonstrating different programming concepts such as summing integers, checking for prime numbers, printing even numbers, calculating factorials, and generating Fibonacci numbers. Each exercise includes code snippets with comments explaining the functionality. The exercises cover basic input/output operations, loops, and functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Bài thực hành 7

The document contains a series of C programming exercises, each demonstrating different programming concepts such as summing integers, checking for prime numbers, printing even numbers, calculating factorials, and generating Fibonacci numbers. Each exercise includes code snippets with comments explaining the functionality. The exercises cover basic input/output operations, loops, and functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Bài 1:

1 #include <stdio.h>
2 // Bài1: Tinh tong 10 so nguyen bat ki duoc nhap tu ban phim
3 int main() {
4 int numbers[10]; // Mang luu 10 so nguyen
5 int sum = 0; // Bien luu tong cac so
6
7 // Nhap 10 so nguyen tu ban phim
8 printf("Nhap 10 so nguyen:\n");
9 for (int i = 0; i < 10; i++) {
10 printf("Nhap so thu %d: ", i + 1);
11 scanf("%d", &numbers[i]); // Nhap so tu ban phim
12 sum += numbers[i]; // Cong don vao tong
13 }
14
15 // In tong cac so da nhap
16 printf("Tong cua 10 so nguyen la: %d\n", sum);
17
18 return 0;
19 }
Bai2:

#include <stdio.h>
1
// Bai2 Kiem tra mot so co la so nguyen to hay khong
2
// Ham kiem tra so nguyen to
3
int isPrime(int n) {
4
if (n < 2) return 0; // So nho hon 2 khong phai so nguyen to
5
for (int i = 2; i * i <= n; i++) {
6
if (n % i == 0) return 0; // Neu n chia het cho i thi khong phai
7
so nguyen to
8
}
9
return 1; // Neu khong chia het cho so nao thi la so nguyen to
10
}
11
12
int main() {
13
int n;
14
15
// Nhap so nguyen tu ban phim
16
printf("Nhap mot so nguyen: ");
17
scanf("%d", &n);
18
19
// Kiem tra va in ket qua
20
if (isPrime(n)) {
21
printf("%d la so nguyen to.\n", n);
22
} else {
23
printf("%d khong phai la so nguyen to.\n", n);
24
}
25
26
return 0;
27
}

Bai 3:

#include <stdio.h>
1
// Bai 3: In ra cac so duong chan tu 1 den 20 tren cung mot dong, cách
2
nhau tab
3
int main() {
4
for (int i = 2; i <= 20; i += 2) {
5
printf("%d\t", i);
6
}
7
8
printf("\n");
9
return 0;
10
}
Bai 4:

1
#include <stdio.h>
2
3
int main() {
4
int sum = 0;
5
for (int i = 1; i <= 100; i += 2) {
6
sum += i;
7
}
8
printf("Tong cac so le tu 1 den 100 la: %d\n", sum);
9
10

Bai 5:

1 #include <stdio.h>
2 // Cau 5: Viet chuong trinh nhap vao so n va in ra cac uoc cua so n do
3 int main() {
4 int n;
5 printf("Nhap mot so nguyen n: ");
6 scanf("%d", &n);
7 printf("Cac uoc cua %d la: ", n);
8 for (int i = 1; i <= n; i++) {
9 if (n % i == 0) {
10 printf("%d ", i);
11 }
12 }
13 printf("\n");
14 }
Bai 6.1:

1 #include <stdio.h>
2 // Cau 6.1: Tim uoc chung lon nhat, boi chung nho nhat cua hai so nguyen
3 // Ham tim UCLN su dung thuat toan Euclid
4 int ucln(int a, int b) {
5 while (b != 0) {
6 int temp = b;
7 b = a % b;
8 a = temp;
9 }
10 return a;
11 }
12
13 // Ham tim BCNN
14 int bcnn(int a, int b) {
15 return (a * b) / ucln(a, b); // BCNN = (a * b) / UCLN
16 }
17
18 int main() {
19 int a, b;
20
21 // Nhap hai so nguyen tu ban phim
22 printf("Nhap hai so nguyen a va b: ");
23 scanf("%d %d", &a, &b);
24
25 // Tinh UCLN va BCNN
26 int uoc = ucln(a, b);
27 int boi = bcnn(a, b);
28
29 // In ket qua
30 printf("UCLN cua %d va %d la: %d\n", a, b, uoc);
31 printf("BCNN cua %d va %d la: %d\n", a, b, boi);
32
33 return 0;
34 }
Bai 6.2:

1 #include <stdio.h>
2 // Cau 6..2 Viet chuong trinh kiem tra 1 so co la so hoan hao khong?
3 // Ham kiem tra so hoan hao
4 int isPerfectNumber(int n) {
5 if (n <= 0) return 0; // So am khong la so hoan hao
6
7 int sum = 0;
8
9 // Tinh tong tat ca cac uoc cua n
10 for (int i = 1; i <= n; i++) {
11 if (n % i == 0) {
12 sum += i; // Cong don uoc vao tong
13 }
14 }
15
16 // Kiem tra dieu kien so hoan hao
17 return (sum == 2 * n);
18 }
19
20 int main() {
21 int n;
22 printf("Nhap mot so nguyen duong: ");
23 scanf("%d", &n);
24 if (isPerfectNumber(n)) {
25 printf("%d la so hoan hao.\n", n);
26 } else {
27 printf("%d khong phai la so hoan hao.\n", n);
28 }
29
30 return 0;
31 }
Bai 7:

1 #include <stdio.h>
2 // Bài 7: Viet chuong trinh tinh S = 1 + 1/2 + 1/3 + … + 1/N
3 int main() {
4 int N;
5 double S = 0.0; // Khai bao bien tong S kieu double de luu so thuc
6
7 // Nhap N tu ban phim
8 printf("Nhap so nguyen duong N: ");
9 scanf("%d", &N);
10
11 // Kiem tra dieu kien N > 0
12 if (N <= 0) {
13 printf("N phai la so nguyen duong!\n");
14 return 1; // Thoat chuong trinh neu N <= 0
15 }
16
17 // Tinh tong S
18 for (int i = 1; i <= N; i++) {
19 S += 1.0 / i; // Chia so thuc de tranh mat du lieu
20 }
21
22 // In ket qua
23 printf("Tong S = %.6f\n", S);
24
25 return 0;
26 }
Bai 8:

#include <stdio.h>
1
2
int main() {
3
int n;
4
long long factorial = 1; // Su dung long long de tranh tran so voi n
5
lon
6
7
// Nhap so n tu ban phim
8
printf("Nhap so nguyen duong n: ");
9
scanf("%d", &n);
10
11
// Kiem tra dieu kien n >= 0
12
if (n < 0) {
13
printf("Khong ton tai giai thua cua so am!\n");
14
return 1; // Thoat chuong trinh neu n < 0
15
}
16
17
// Tinh giai thua
18
for (int i = 1; i <= n; i++) {
19
factorial *= i;
20
}
21
22
// In ket qua
23
printf("%d! = %lld\n", n, factorial);
24
25
return 0;
26
}
Bai 9:

#include <stdio.h>
1
// Tinh tong: S = 1/1! + 1/2! +... + 1/n! . Voi gia tri cua n nhap vào
2
tu ban phim.
3
// Ham tinh giai thua cua n
4
long long factorial(int n) {
5
long long fact = 1;
6
for (int i = 1; i <= n; i++) {
7
fact *= i;
8
}
9
return fact;
10
}
11
12
int main() {
13
int n;
14
double S = 1.0; // Khai bao S kieu double de luu so thuc
15
16
// Nhap n tu ban phim
17
printf("Nhap so nguyen duong n: ");
18
scanf("%d", &n);
19
20
// Kiem tra dieu kien n >= 0
21
if (n < 0) {
22
printf("n phai la so nguyen duong!\n");
23
return 1; // Thoat chuong trinh neu n < 0
24
}
25
26
// Tinh tong S
27
for (int i = 2; i <= n; i++) {
28
S += 1.0 / factorial(i); // Cong don 1 / i!
29
}
30
31
// In ket qua
32
printf("Tong S = %.6f\n", S);
33
34
return 0;
35
}
Bai 10:

1 #include <stdio.h>
2 // Cau 10 : In ra so Fibonacci thu n
3 int fibonacci(int n) {
4 if (n == 1 || n == 2) return 1; // Truong hop dac biet
5
6 int a = 1, b = 1, fib;
7 for (int i = 3; i <= n; i++) {
8 fib = a + b;
9 a = b;
10 b = fib;
11 }
12 return fib;
13 }
14
15 int main() {
16 int n;
17
18 // Nhap n tu ban phim
19 printf("Nhap so n: ");
20 scanf("%d", &n);
21
22 // Kiem tra dieu kien n > 0
23 if (n <= 0) {
24 printf("Vui long nhap n > 0!\n");
25 return 1; // Thoat chuong trinh neu n <= 0
26 }
27
28 // Tinh va in so Fibonacci thu n
29 printf("So Fibonacci thu %d la: %d\n", n, fibonacci(n));
30
31 return 0;
32 }

You might also like