Python程序反转给定数字(2种不同方式)
Take input number from the user and print its reverse.
從用戶處獲取輸入號(hào)碼并打印其反面。
Example:
例:
Input:12345Output:54321Here, we are implementing program to reversing a given number using 2 different ways.
在這里,我們正在實(shí)施程序,以使用2種不同的方式來(lái)反轉(zhuǎn)給定的數(shù)字 。
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) 逆轉(zhuǎn)數(shù)字的著名方法 :從用戶輸入并將其類型轉(zhuǎn)換為整數(shù),然后在循環(huán)中循環(huán)直到num在循環(huán)內(nèi)不為零:
Find out the remainder.
找出其余的。
Using this: rev_num = rev_num * 10 + remainder.
使用這個(gè):rev_num = rev_num * 10 +余數(shù)。
Update that number by diving by 10.
通過(guò)跳水10來(lái)更新該數(shù)字。
After coming out of the loop printing the reverse number.
退出循環(huán)后,打印反向編號(hào)。
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) 制作一個(gè)用于反轉(zhuǎn)Number的用戶定義函數(shù) :從用戶那里輸入輸入并將其類型轉(zhuǎn)換為整數(shù),然后調(diào)用verseNum()函數(shù)。
Inside the function:
函數(shù)內(nèi)部:
Iterate in the loop till num does not become zero:
在循環(huán)中迭代,直到num不為零:
Find out the remainder.
找出其余的。
Using this: rev_num = rev_num * 10 + remainder.
使用這個(gè):rev_num = rev_num * 10 +余數(shù)。
Update that number by diving by 10.
通過(guò)跳水10來(lái)更新該數(shù)字。
After coming out of the loop returning the reverse number to the main.
退出循環(huán)后,將反向編號(hào)返回到主編號(hào)。
Output
輸出量
Enter a number: 12345 Reverse number is: 54321翻譯自: https://www.includehelp.com/python/program-to-reverse-a-given-number-2-different-ways.aspx
總結(jié)
以上是生活随笔為你收集整理的Python程序反转给定数字(2种不同方式)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 杭州看多囊卵巢最好的医院推荐
- 下一篇: python变量分配内存_Python