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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 程序:查找兩個數字的 LCM > 原文: [https://www.programiz.com/java-programming/examples/lcm](https://www.programiz.com/java-programming/examples/lcm) #### 在此程序中,您將學習使用 GCD 而不是 GCD 查找兩個數字的 lcm。 這是使用 Java 中的`for`和`while`循環完成的。 兩個整數的 LCM 是可以被兩個數字完全除(沒有余數)的最小正整數。 ## 示例 1:使用`while`循環和`if`語句的 LCM ```java public class LCM { public static void main(String[] args) { int n1 = 72, n2 = 120, lcm; // maximum number between n1 and n2 is stored in lcm lcm = (n1 > n2) ? n1 : n2; // Always true while(true) { if( lcm % n1 == 0 && lcm % n2 == 0 ) { System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm); break; } ++lcm; } } } ``` 運行該程序時,輸出為: ```java The LCM of 72 and 120 is 360. ``` 在此程序中,將找到其 LCM 的兩個數字分別存儲在變量`n1`和`n2`中。 然后,我們最初將`lcm`設置為兩個數字中的最大值。 這是因為 LCM 不能小于最大數量。 在無限`while`循環(`while(true)`)內,我們檢查`lcm`是否完美地劃分了`n1`和`n2`。 如果是這樣,我們已經找到 LCM。 我們打印 LCM 并使用`break`語句退出`while`循環。 否則,我們將`lcm`加 1,然后重新測試除數條件。 * * * 我們還可以使用 GCD 通過以下公式查找兩個數字的 LCM: ```java LCM = (n1 * n2) / GCD ``` 如果您不知道如何用 Java 計算 GCD,請檢查 [Java 程序以找到兩個數字](/java-programming/examples/hcf-gcd "How to find GCD of two numbers in Java?")的 GCD。 ## 示例 2:使用 GCD 計算 LCM ```java public class LCM { public static void main(String[] args) { int n1 = 72, n2 = 120, gcd = 1; for(int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if(n1 % i == 0 && n2 % i == 0) gcd = i; } int lcm = (n1 * n2) / gcd; System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm); } } ``` 該程序的輸出與示例 1 相同。 在這里,在`for`循環內,我們計算兩個數字的 GCD - `n1`和`n2`。 計算后,我們使用上面的公式來計算 LCM。
                  <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>

                              哎呀哎呀视频在线观看