2018年1月9日 星期二

Python: Calculate the sum of the digits in an integer

Python: Calculate the sum of the digits in an integer

Python Basic: Exercise-68 with Solution

Write a Python program to calculate the sum of the digits in an integer.
Sample Solution:-
num = int(input("Input a four digit numbers: "))
x  = num //1000
x1 = (num - x*1000)//100
x2 = (num - x*1000 - x1*100)//10
x3 = num - x*1000 - x1*100 - x2*10
print("The sum of digits in the number is", x,"+",x1,"+",x2,"+",x3,"=",x+x1+x2+x3)

========== RESTART: F:/Python_APSC/py-ex-basic-68.py ============
Input a four digit numbers: 4578
The sum of digits in the number is 4 + 5 + 7 + 8 = 24
>>> 

沒有留言:

張貼留言

OPEN DATA AQI 空氣品質監測網 + NODE-RED

OPEN DATA AQI 空氣品質監測網 + NODE-RED  首頁 - 空氣品質監測網  https://airtw.moenv.gov.tw/ https://data.gov.tw/datasets/search?p=1&size=10&s=_score...