<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之旅 廣告
                # C 程序:顯示兩個間隔之間的質數 > 原文: [https://www.programiz.com/c-programming/examples/prime-number-intervals](https://www.programiz.com/c-programming/examples/prime-number-intervals) #### 在此示例中,您將學習如何打印用戶輸入的兩個數字之間的所有質數。 要理解此示例,您應該了解以下 [C 編程](/c-programming "C tutorial")主題: * [C `if...else`語句](/c-programming/c-if-else-statement) * [C `for`循環](/c-programming/c-for-loop) * [C `break`和`continue`](/c-programming/c-break-continue-statement) * * * ## 顯示兩個間隔之間的質數 ```c #include <stdio.h> int main() { int low, high, i, flag; printf("Enter two numbers(intervals): "); scanf("%d %d", &low, &high); printf("Prime numbers between %d and %d are: ", low, high); // iteration until low is not equal to high while (low < high) { flag = 0; // ignore numbers less than 2 if (low <= 1) { ++low; continue; } // if low is a non-prime number, flag will be 1 for (i = 2; i <= low / 2; ++i) { if (low % i == 0) { flag = 1; break; } } if (flag == 0) printf("%d ", low); // to check prime for the next number // increase low by 1 ++low; } return 0; } ``` **輸出** ```c Enter two numbers(intervals): 20 50 Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47 ``` * * * 在此程序中,`while`循環被迭代(`high-low-1`)次。 在每次迭代中,檢查`low`是否為質數,并且將`low`的值增加`1`,直到`low`等于`high`。 訪問此頁面以了解有關如何[檢查數字是否為質數](https://www.programiz.com/c-programming/examples/prime-number)的更多信息。 如果用戶首先輸入較大的數字,則上述程序將無法正常工作。 您可以通過[交換數字](https://www.programiz.com/c-programming/examples/swapping)來解決此問題。 * * * ## 首先輸入較大的數字時顯示質數 ```c #include <stdio.h> int main() { int low, high, i, flag, temp; printf("Enter two numbers(intervals): "); scanf("%d %d", &low, &high); // swap numbers if low is greather than high if (low > high) { temp = low; low = high; high = temp; } printf("Prime numbers between %d and %d are: ", low, high); while (low < high) { flag = 0; // ignore numbers less than 2 if (low <= 1) { ++low; continue; } for (i = 2; i <= low / 2; ++i) { if (low % i == 0) { flag = 1; break; } } if (flag == 0) printf("%d ", low); ++low; } return 0; } ``` 訪問此頁面以了解如何通過創建用戶定義的函數來[顯示兩個時間間隔之間的所有質數](https://www.programiz.com/c-programming/examples/prime-interval-function)。
                  <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>

                              哎呀哎呀视频在线观看