<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # C 程序:查找兩個數字的 GCD > 原文: [https://www.programiz.com/c-programming/examples/hcf-gcd](https://www.programiz.com/c-programming/examples/hcf-gcd) #### 使用循環和決策語句以不同方式計算兩個整數(正整數和負整數)的 GCD 的示例。 要理解此示例,您應該了解以下 [C 編程](/c-programming "C tutorial")主題: * [C 編程運算符](/c-programming/c-operators) * [C `for`循環](/c-programming/c-for-loop) * [C `if...else`語句](/c-programming/c-if-else-statement) * [C `while`和`do...while`循環](/c-programming/c-do-while-loops) * * * 兩個整數的 HCF 或 GCD 是可以完全除以兩個數(沒有余數)的最大整數。 找到 C 編程中最大公約數的方法有很多。 ## 例 1:使用`for`循環和`if`語句的 GCD ```c #include <stdio.h> int main() { int n1, n2, i, gcd; printf("Enter two integers: "); scanf("%d %d", &n1, &n2); for(i=1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if(n1%i==0 && n2%i==0) gcd = i; } printf("GCD of %d and %d is %d", n1, n2, gcd); return 0; } ``` 在該程序中,用戶輸入的兩個整數存儲在變量`n1`和`n2`中。然后,迭代`for`循環,直到`i`小于[ `n1`和`n2`。 在每次迭代中,如果`n1`和`n2`都可被`i`完全整除,則`i`的值將分配給`gcd`。 當`for`循環完成時,兩個數字的最大公約數將存儲在變量`gcd`中。 ## 示例 2:使用`while`循環和`if...else`語句的 GCD ```c #include <stdio.h> int main() { int n1, n2; printf("Enter two positive integers: "); scanf("%d %d",&n1,&n2); while(n1!=n2) { if(n1 > n2) n1 -= n2; else n2 -= n1; } printf("GCD = %d",n1); return 0; } ``` **輸出** ```c Enter two positive integers: 81 153 GCD = 9 ``` 這是查找 GCD 的更好方法。 在此方法中,從較大的整數中減去較小的整數,然后將結果分配給保存較大整數的變量。 繼續該過程,直到`n1`和`n2`相等為止。 僅當用戶輸入正整數時,以上兩個程序才能按預期工作。 這是第二個示例的一些修改,可以找到正整數和負整數的 GCD。 ## 例 3:正負數的 GCD ```c #include <stdio.h> int main() { int n1, n2; printf("Enter two integers: "); scanf("%d %d",&n1,&n2); // if user enters negative number, sign of the number is changed to positive n1 = ( n1 > 0) ? n1 : -n1; n2 = ( n2 > 0) ? n2 : -n2; while(n1!=n2) { if(n1 > n2) n1 -= n2; else n2 -= n1; } printf("GCD = %d",n1); return 0; } ``` **輸出** ```c Enter two integers: 81 -153 GCD = 9 ``` 您也可以使用[遞歸找到 GCD](/c-programming/examples/hcf-recursion "GCD using recursion") 。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看