forked from detro/coding-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleast_common_multiple.c
41 lines (34 loc) · 910 Bytes
/
least_common_multiple.c
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/types.h>
u_long least_common_multiple(u_long a, u_long b) {
u_long multi_a = a, factor_a = 1;
u_long multi_b = b, factor_b = 1;
// Guarding from 0 input - thanks John ;)
if (a == 0 || b == 0) {
return 0;
}
while(multi_a != multi_b) {
if (multi_a < multi_b) {
multi_a = a * ++factor_a;
} else if (multi_b < multi_a) {
multi_b = b * ++factor_b;
}
}
return multi_a; //< or multi_b, as they are the same :)
}
int main(int argc, char** argv)
{
u_long a, b;
// Check the Input
if (argc == 3) {
a = strtol(argv[1], NULL, 10);
b = strtol(argv[2], NULL, 10);
} else
return 1;
printf("Least Common Multiple for '%lu' and '%lu' is '%lu'\n",
a, b,
least_common_multiple(a, b));
return 0;
}