<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 程序:查找兩個數字的 GCD > 原文: [https://beginnersbook.com/2018/09/java-program-to-find-gcd-of-two-numbers/](https://beginnersbook.com/2018/09/java-program-to-find-gcd-of-two-numbers/) 兩個數字的 **GCD(最大公約數)**是最大的正整數,它將兩個數字整除而不留任何余數。例如。 30 和 45 的 GCD 是 15。GCD 也稱為 HCF(最高公因子)。在[教程](https://beginnersbook.com/java-tutorial-for-beginners-with-examples/)中,我們將編寫幾個不同的 [Java 程序](https://beginnersbook.com/2017/09/java-examples/)來找出兩個數字的 GCD。 ## 如何在紙上找出 GCD? 為了找出兩個數字的 GCD,我們將公因子乘以如下圖所示: ![Finding GCD of numbers in Java](https://img.kancloud.cn/60/b1/60b1d13ee4d6f65bf3e80017ba633d8d_600x200.jpg) ## 示例 1:使用`for`循環查找兩個數字的 GCD 在這個例子中,我們使用[`for`循環](https://beginnersbook.com/2015/03/for-loop-in-java-with-example/)找到兩個給定數字的 GCD。 我們正在運行一個 for 循環,從 1 到較小的數字和內部循環,我們將這兩個數字除以循環計數器`i`,范圍從 1 到較小的數字值。如果`i`的值除以兩個數字而沒有余數,那么我們將該值賦給變量`gcd`。在循環結束時,變量`gcd`將具有最大數字,該數字除以兩個數字而沒有余數。 ```java public class GCDExample1 { public static void main(String[] args) { //Lets take two numbers 55 and 121 and find their GCD int num1 = 55, num2 = 121, gcd = 1; /* loop is running from 1 to the smallest of both numbers * In this example the loop will run from 1 to 55 because 55 * is the smaller number. All the numbers from 1 to 55 will be * checked. A number that perfectly divides both numbers would * be stored in variable "gcd". By doing this, at the end, the * variable gcd will have the largest number that divides both * numbers without remainder. */ for(int i = 1; i <= num1 && i <= num2; i++) { if(num1%i==0 && num2%i==0) gcd = i; } System.out.printf("GCD of %d and %d is: %d", num1, num2, gcd); } } ``` 輸出: ```java GCD of 55 and 121 is: 11 ``` ## 示例 2:使用`while`循環查找兩個數字的 GCD 讓我們使用[`while`循環](https://beginnersbook.com/2015/03/while-loop-in-java-with-examples/)編寫相同的程序。在這里,我們采用不同的方法來尋找 gcd。在這個程序中,我們從較大的數字中減去較小的數字,直到它們變得相同。在循環結束時,數字的值將相同,并且該值將是這些數字的 GCD。 ```java public class GCDExample2 { public static void main(String[] args) { int num1 = 55, num2 = 121; while (num1 != num2) { if(num1 > num2) num1 = num1 - num2; else num2 = num2 - num1; } System.out.printf("GCD of given numbers is: %d", num2); } } ``` 輸出: ```java GCD of given numbers is: 11 ``` ## 示例 3:查找兩個輸入(由用戶輸入)數字的 GCD 在這個例子中,我們使用`Scanner`來[從用戶](https://beginnersbook.com/2014/07/java-program-to-get-input-from-user/)獲取輸入。用戶將輸入兩個數字的值,程序將找到這些輸入數字的 GCD。 ```java import java.util.Scanner; public class GCDExample3 { public static void main(String[] args) { int num1, num2; //Reading the input numbers Scanner scanner = new Scanner(System.in); System.out.print("Enter first number:"); num1 = (int)scanner.nextInt(); System.out.print("Enter second number:"); num2 = (int)scanner.nextInt(); //closing the scanner to avoid memory leaks scanner.close(); while (num1 != num2) { if(num1 > num2) num1 = num1 - num2; else num2 = num2 - num1; } //displaying the result System.out.printf("GCD of given numbers is: %d", num2); } } ``` 輸出: ```java Enter first number:30 Enter second number:250 GCD of given numbers is: 10 ``` 下面是幾個相關的 java 例子: 1. [Java 程序:找到 factorial 數](https://beginnersbook.com/2017/09/java-program-to-find-factorial-using-for-and-while-loop/) 2. [Java 程序:顯示 Fibonacci 系列](https://beginnersbook.com/2017/09/java-program-to-display-fibonacci-series-using-loops/) 3. [Java 程序:找到三個數字中最大的數字](https://beginnersbook.com/2017/09/java-program-to-find-largest-of-three-numbers/)
                  <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>

                              哎呀哎呀视频在线观看