forked from thantrieu/LearnC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbai2.8.c
More file actions
39 lines (34 loc) · 644 Bytes
/
bai2.8.c
File metadata and controls
39 lines (34 loc) · 644 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
int main() {
int a, b;
printf("Nhap a, b > 0: ");
scanf("%d%d", &a, &b);
if(a >= 0 && b >= 0) {
if(a == 0 && b == 0) {
printf("Khong ton tai UCLN, BCNN\n");
} else if(a == 0 || b == 0) {
printf("Khong co BCNN, UCLN = %d", (a == 0) ? b : a);
} else { // a, b > 0
int bc = a * b;
// cach 1:
// while(a != b) {
// if(a > b) {
// a = a - b;
// } else {
// b = b - a;
// }
// }
// cach 2:
while(a != 0) {
int x = a;
a = b % a;
b = x;
}
printf("UCLN = %d\n", b);
printf("BCNN = %d\n", bc / b);
}
} else {
printf("Nhap a, b > 0");
}
return 0;
}