forked from AllAlgorithms/c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleast_common_multiple.c
More file actions
42 lines (32 loc) · 836 Bytes
/
least_common_multiple.c
File metadata and controls
42 lines (32 loc) · 836 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
40
41
42
#include <stdio.h>
// Returns -1 on error, > 0 on success
int get_least_common_multiple(int a, int b) {
int maximum = 0;
if (a <= 0 || b <= 0) {
return -1;
}
maximum = (a > b)? a : b;
while (1) {
if ((maximum % a == 0) && (maximum % b == 0)) {
break;
}
maximum++;
}
return maximum;
}
int main()
{
int a = 0;
int b = 0;
int least_common_multiple = 0;
while (1) {
printf("give two integers: \n");
scanf("%d %d", &a, &b);
if (-1 == (least_common_multiple = get_least_common_multiple(a, b))) {
fprintf(stderr, "Failed to get least common multiple for %d and %d!\n", a, b);
continue;
}
printf(" least common multiple of %d and %d is %d\n", a, b, least_common_multiple);
}
return 0;
}