函式_最大公因數

 avatar
user_3763047219
c_cpp
2 years ago
533 B
0
Indexable
Never
void gcd(int a, int b);

#include <stdio.h>
void gcd(int a, int b) {
    int a2 = a, b2 = b;
    while (a != 0 && b != 0) {
        if (a > b) {
            a = a - a / b * b;
        }
        else {
            b = b - b / a * a;
        }
    }
    if (a == 0) {
        printf("%d與%d的最大公因數為%d", a2, b2, b);
    }
    else {
        printf("%d與%d的最大公因數為%d", a2, b2, a);
    }
}




int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    gcd(a, b);

    return 0;
}