Number to English Words
Write a program to convert a non-negative integer
The input will be a single line containing an integer
The output should be a single line containing the representation of the English words of number
For example, if the given number is 123, your code should print the English words representation, which is
One Hundred Twenty Three
Sample Input 1
123
Sample Output 1
One Hundred Twenty Three
Sample Input 2
10005
Sample Output 2
Ten Thousand Five
Sample Input 3
1234567891
Sample Output 3
One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One
def num3d(n):
digits=['', 'One', 'Two', 'Three', 'Four', 'Five',
'Six', 'Seven', 'Eight', 'Nine']
tenths = ['', 'Ten', 'Twenty', 'Thirty', 'Fourty', 'Fifty',
'Sixty', 'Seventy', 'Eighty', 'Ninty']
teenths = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen',
'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']
hundred = n // 100
s = digits[hundred]
if s != '':
s += ' Hundred'
n %= 100
if s:
s += ' '
if (10 <= n <= 19):
s += teenths[n-10]
else:
if n >= 20:
s += tenths[n//10]
n %= 10
if n != 0:
s += ' '
s += digits[n]
return s
def num(n):
if n == 0:
return 'Zero'
n3 = n // 1000000000
n %= 1000000000
if n3 != 0:
s = num3d(n3) + ' Billion'
else:
s = ''
n3 = n // 1000000
n %= 1000000
if n3 != 0:
if s:
s += ' '
s += num3d(n3) + ' Million'
n3 = n // 1000
n %= 1000
if n3 != 0:
if s:
s += ' '
s += num3d(n3) + ' Thousand'
if n != 0:
if s:
s += ' '
s += num3d(n)
return s
def main():
n = int(input())
print(num(n))
main()
Comments
Leave a comment