<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/cpp-programming/examples/palindrome-number](https://www.programiz.com/cpp-programming/examples/palindrome-number) #### 該程序使用`while`循環反轉整數(由用戶輸入)。 然后,如果使用`if`語句檢查反向編號是否等于原始編號。 要理解此示例,您應該了解以下 [C++ 編程](/cpp-programming "C++ tutorial")主題: * [C++ `while`和`do...while`循環](/cpp-programming/do-while-loop) * [C++ `if`,`if...else`和嵌套`if...else`](/cpp-programming/if-else) * * * 該程序從用戶那里獲取一個整數,并且該整數被取反。 如果反轉的整數等于用戶輸入的整數,則該數字為回文,如果不是,則該數字不是回文。 ## 示例:檢查回文數 ```cpp #include <iostream> using namespace std; int main() { int n, num, digit, rev = 0; cout << "Enter a positive number: "; cin >> num; n = num; do { digit = num % 10; rev = (rev * 10) + digit; num = num / 10; } while (num != 0); cout << " The reverse of the number is: " << rev << endl; if (n == rev) cout << " The number is a palindrome."; else cout << " The number is not a palindrome."; return 0; } ``` **輸出** ```cpp Enter a positive number: 12321 The reverse of the number is: 12321 The number is a palindrome. ``` ```cpp Enter a positive number: 12331 The reverse of the number is: 13321 The number is not a palindrome. ``` 在上述程序中,要求使用輸入一個正數,該正數存儲在變量`num`中。 然后將數字保存到另一個變量`n`中,以便在原始數字反轉后對其進行檢查。 在`do...while`循環內,使用代碼`digit = num % 10;`分隔數字的最后一位。 然后將此數字添加到`rev`變量中。 在將`num`添加到`rev`之前,我們首先需要將`rev`變量中的當前數據乘以 10,以便將數字添加到數字中的第`n`位。 例如:在數字 123 中,3 在個位,2 在十位,1 在百位。 因此,要在 123 之后再添加一個數字 4,我們需要將當前數字向左移動,所以現在 1 在千位置,2 在百位, 3 十位,4 在個位。 通過將 123 乘以 10(得到 1230)并加上數字 4(得到 1234),可以很容易地做到這一點。在上面的代碼中也是如此。 當`do while`循環最終結束時,我們在`rev`中有一個相反的數字。 然后將該數字與原始數字`n`進行比較。 如果數字相等,則原始數字為回文,否則為非。
                  <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>

                              哎呀哎呀视频在线观看