<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 程序:查找前 n 個自然數的和 > 原文: [https://beginnersbook.com/2017/10/c-program-to-find-the-sum-of-first-n-natural-numbers/](https://beginnersbook.com/2017/10/c-program-to-find-the-sum-of-first-n-natural-numbers/) 程序查找前`n`個自然數的總和。我們將看到計算自然數的總和的兩個 C 程序。在第一個 C 程序中,我們使用`for`循環查找總和,在第二個程序中,我們使用`while`循環執行相同操作。 要了解這些程序,您應該熟悉以下 [C 編程](https://beginnersbook.com/2014/01/c-tutorial-for-beginners-with-examples/)概念: 1. [C 編程`for`循環](https://beginnersbook.com/2014/01/c-for-loop/) 2. [C 編程`while`循環](https://beginnersbook.com/2014/01/c-while-loop/) ## 示例 1:使用`for`循環查找自然數之和的程序 用戶輸入`n`的值,程序使用`for`循環計算前`n`個自然數的總和。 ```c #include <stdio.h> int main() { int n, count, sum = 0; printf("Enter the value of n(positive integer): "); scanf("%d",&n); for(count=1; count <= n; count++) { sum = sum + count; } printf("Sum of first %d natural numbers is: %d",n, sum); return 0; } ``` 輸出: ```c Enter the value of n(positive integer): 6 Sum of first 6 natural numbers is: 21 ``` ## 示例 2:使用`while`循環查找自然數的總和 ```c #include <stdio.h> int main() { int n, count, sum = 0; printf("Enter the value of n(positive integer): "); scanf("%d",&n); /* When you use while loop, you have to initialize the * loop counter variable before the loop and increment * or decrement it inside the body of loop like we did * for the variable "count" */ count=1; while(count <= n){ sum = sum + count; count++; } printf("Sum of first %d natural numbers is: %d",n, sum); return 0; } ``` 輸出: ```c Enter the value of n(positive integer): 7 Sum of first 7 natural numbers is: 28 ``` 看看這些相關的 C 程序: 1. [C 程序:查找數組元素之和](https://beginnersbook.com/2014/06/c-program-to-find-sum-of-array-elements-using-pointers-recursion-functions/) 2. [C 程序:相加兩個數字](https://beginnersbook.com/2017/09/c-program-to-add-two-numbers/) 3. [C 程序:查找商和余數](https://beginnersbook.com/2017/09/c-program-to-find-quotient-and-remainder/) 4. [C 程序:交換兩個數字](https://beginnersbook.com/2017/09/c-program-to-swap-two-numbers/) 5. [C 程序:查找數字的階乘](https://beginnersbook.com/2014/06/c-program-to-find-factorial-of-number-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>

                              哎呀哎呀视频在线观看