-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGGT.java
More file actions
31 lines (25 loc) · 880 Bytes
/
Copy pathGGT.java
File metadata and controls
31 lines (25 loc) · 880 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
public class GGT {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
if (a < 0 || b < 0) {
System.out.println("nur positive ganze Zahlen als Argumente erlaubt");
return;
} else {
int m = a;
int n = b;
int r;
while (n != 0) {
if (m < n) {
int c = m; // zwischenspeichert m in c
m = n; // vertauscht m mit n
n = c; // vertauscht n mit m
}
r = m - n; // zieht größere Zahl von der kleineren ab
m = n; // m wird n
n = r; // n wird irgendwann 0 wenn es einen ggT gibt
}
System.out.println("ggT(" + a + ", " + b + ") = " + m);
}
}
}