<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之旅 廣告
                # Python 程序:檢查阿姆斯特朗數 > 原文: [https://www.programiz.com/python-programming/examples/armstrong-number](https://www.programiz.com/python-programming/examples/armstrong-number) #### 在此示例中,您將學習檢查 n 位整數是否是阿姆斯特朗數。 要理解此示例,您應該了解以下 [Python 編程](/python-programming "Python tutorial")主題: * [Python `if...else`語句](/python-programming/if-elif-else) * [Python `while`循環](/python-programming/while-loop) * * * 一個正整數稱為阿姆斯特朗數`n`,如果 ```py abcd... = an + bn + cn + dn + ... ``` 對于 3 位的阿姆斯特朗數字,每個數字的立方數之和等于數字本身。 例如: ```py 153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number. ``` ## 源代碼:檢查 3 位阿姆斯特朗數 ```py # Python program to check if the number is an Armstrong number or not # take input from the user num = int(input("Enter a number: ")) # initialize sum sum = 0 # find the sum of the cube of each digit temp = num while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 # display the result if num == sum: print(num,"is an Armstrong number") else: print(num,"is not an Armstrong number") ``` **輸出 1** ```py Enter a number: 663 663 is not an Armstrong number ``` **輸出 2** ```py Enter a number: 407 407 is an Armstrong number ``` 在這里,我們要求用戶輸入一個數字,然后檢查它是否是一個阿姆斯特朗數字。 我們需要計算每個數字的立方和。 因此,我們將總和初始化為 0,并使用[模運算符`%`](/python-programming/operators#arithmetic_operators)獲得每個數字。 將數字除以 10 所得的余數是該數字的最后一位。 我們使用指數運算符獲取多維數據集。 最后,我們將總和與原始數字進行比較,得出結論,如果它們相等,則為阿姆斯特朗數字。 ## 源代碼:檢查 n 位阿姆斯特朗數 ```py num = 1634 # Changed num variable to string, # and calculated the length (number of digits) order = len(str(num)) # initialize sum sum = 0 # find the sum of the cube of each digit temp = num while temp > 0: digit = temp % 10 sum += digit ** order temp //= 10 # display the result if num == sum: print(num,"is an Armstrong number") else: print(num,"is not an Armstrong number") ``` 您可以在源代碼中更改`num`的值,然后再次運行以對其進行測試。
                  <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>

                              哎呀哎呀视频在线观看