C語言計算最大公約數

下面是使用C語言來計算最大公約數的事例代碼。

/* Program to find the greatest common divisor of two nonnegative integer values */

#include <stdio.h>
int main(void)
{
    int u, v, temp;

    printf("Please type in two nonnegative integers.\n");
    scanf("%i%i", &u, &v);

    while(v!=0)
    {
        temp = u % v;
        u = v;
        v = temp;
    }

    printf("Their greatest common divisor is %i\n", u);

    return 0;
}
發佈了33 篇原創文章 · 獲贊 15 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章