Python程序反转给定数字(2种不同方式)
Take input number from the user and print its reverse.
從用戶處獲取輸入號碼并打印其反面。
Example:
例:
Input:12345Output:54321Here, we are implementing program to reversing a given number using 2 different ways.
在這里,我們正在實施程序,以使用2種不同的方式來反轉給定的數字 。
1) Famous approach for reversing the number: Take input from the user and typecast into an integer, then iterate in the loop till num is not become zero, inside the loop:
1) 逆轉數字的著名方法 :從用戶輸入并將其類型轉換為整數,然后在循環中循環直到num在循環內不為零:
- Find out the remainder. - 找出其余的。 
- Using this: rev_num = rev_num * 10 + remainder. - 使用這個:rev_num = rev_num * 10 +余數。 
- Update that number by diving by 10. - 通過跳水10來更新該數字。 
- After coming out of the loop printing the reverse number. - 退出循環后,打印反向編號。 
Output
輸出量
Enter a number: 12345 Reverse number is: 543212) Make a user-defined function for reversing the Number: Take input from the user and typecast into integer, thenreverseNum() function call.
2) 制作一個用于反轉Number的用戶定義函數 :從用戶那里輸入輸入并將其類型轉換為整數,然后調用verseNum()函數。
Inside the function:
函數內部:
- Iterate in the loop till num does not become zero: - 在循環中迭代,直到num不為零: 
- Find out the remainder. - 找出其余的。 
- Using this: rev_num = rev_num * 10 + remainder. - 使用這個:rev_num = rev_num * 10 +余數。 
- Update that number by diving by 10. - 通過跳水10來更新該數字。 
- After coming out of the loop returning the reverse number to the main. - 退出循環后,將反向編號返回到主編號。 
Output
輸出量
Enter a number: 12345 Reverse number is: 54321翻譯自: https://www.includehelp.com/python/program-to-reverse-a-given-number-2-different-ways.aspx
總結
以上是生活随笔為你收集整理的Python程序反转给定数字(2种不同方式)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 杭州看多囊卵巢最好的医院推荐
- 下一篇: dnf船长的戒指没用了?
