-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcd
53 lines (40 loc) · 1.03 KB
/
gcd
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
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
int gdc(int n, int m);
int calcGdcFromAllArguments(int argc, char *const *argv, int result);
int main(int argc, char *argv[]) {
int result = 0;
if (argc > 1) {
int n = atoi(argv[0]);
int m = atoi(argv[1]);
result = gdc(n, m);
} else {
printf("Please enter at least two integers as arguments\n");
}
result = calcGdcFromAllArguments(argc, argv, result);
printf("%d", result);
return 0;
}
int calcGdcFromAllArguments(int argc, char *const *argv, int result) {
for (int i = 2; i < argc; i++) {
if (isdigit(*argv[i]) != 0) {
int m = atoi(argv[i]);
result = gdc(result, m);
} else {
printf("%s is not an integer and will be ignored in computation\n", argv[i]);
}
}
return result;
}
int gdc(int n, int m) {
if (m > n) {
return gdc(m, n);
}
if (m == 0) {
return n;
} else {
return gdc(m, n % m);
}
}