diff --git a/22.5.c b/22.5.c new file mode 100644 index 0000000..b88ec00 --- /dev/null +++ b/22.5.c @@ -0,0 +1,40 @@ +#include + +int ggt(int n, int m) { + if (m == 0) { + return n; + } else { + return ggt(m, n % m); + } +} + +int main() { + int zahl, ggt_result; + int first_input = 1; + + printf("Gib nicht-negative ganze Zahlen ein (Ende=0)\n"); + + while (1) { + printf("Zahl: "); + if (scanf("%d", &zahl) != 1 || zahl < 0) { + printf("Ungueltige Eingabe, bitte eine nicht-negative ganze Zahl eingeben.\n"); + while (getchar() != '\n'); + continue; + } + + if (zahl == 0) { + break; + } + + if (first_input) { + ggt_result = zahl; + first_input = 0; + } else { + ggt_result = ggt(ggt_result, zahl); + } + } + + printf("Groesster Gemeinsamer Teiler = %d\n", ggt_result); + + return 0; +}