<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # C 程序:查找兩個數字的 LCM > 原文: [https://www.programiz.com/c-programming/examples/lcm](https://www.programiz.com/c-programming/examples/lcm) #### 在此示例中,您將學習如何計算用戶輸入的兩個數字的 LCM(最小公倍數)。 要理解此示例,您應該了解以下 [C 編程](/c-programming "C tutorial")主題: * [C 編程運算符](/c-programming/c-operators) * [C `if...else`語句](/c-programming/c-if-else-statement) * [C `while`和`do...while`循環](/c-programming/c-do-while-loops) * * * 兩個整數`n1`和`n2`的 LCM 是可被`n1`和`n2`完全除盡的最小正整數。 )。 例如,LCM 為 72 和 120 為 360。 * * * ## LCM 使用`while`和`if` ```c #include <stdio.h> int main() { int n1, n2, min; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); // maximum number between n1 and n2 is stored in min min = (n1 > n2) ? n1 : n2; while (1) { if (min % n1 == 0 && min % n2 == 0) { printf("The LCM of %d and %d is %d.", n1, n2, min); break; } ++min; } return 0; } ``` **輸出** ```c Enter two positive integers: 72 120 The LCM of 72 and 120 is 360. ``` 在該程序中,用戶輸入的整數分別存儲在變量`n1`和`n2`中。 `min`中存儲了`n1`和`n2`中最大的數目。 兩個數字的 LCM 不能小于`min`。 `while`循環的測試表達式始終為`true`。 在每次迭代中,檢查`min`是否可被`n1`和`n2`完全整除。 ```c if (min % n1 == 0 && min % n2 == 0) { ... } ``` 如果此測試條件不成立,則將`min`遞增`1`,并且迭代將繼續進行,直到`if`語句的測試表達式為`true`。 也可以使用以下公式找到兩個數字的 LCM: ```c LCM = (num1*num2)/GCD ``` 了解如何在 C 編程中找到[兩個數字的 GCD](https://www.programiz.com/c-programming/examples/hcf-gcd)。 * * * ## 使用 GCD 計算 LCM ```c #include <stdio.h> int main() { int n1, n2, i, gcd, lcm; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); for (i = 1; i <= n1 && i <= n2; ++i) { // check if i is a factor of both integers if (n1 % i == 0 && n2 % i == 0) gcd = i; } lcm = (n1 * n2) / gcd; printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm); return 0; } ``` **輸出** ```c Enter two positive integers: 72 120 The LCM of two numbers 72 and 120 is 360. ```
                  <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>

                              哎呀哎呀视频在线观看