From d8284e7a7e702b264d3deb2accf52892d55692c3 Mon Sep 17 00:00:00 2001 From: Sophia Date: Thu, 3 Apr 2025 23:07:31 +0200 Subject: [PATCH] Aufgabe 22.5 --- 22.5.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 22.5.c 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; +}