Answer to Question #194327 in Python for mani

Question #194327

Number to English Words


Write a program to convert a non-negative integer

N to its English words representation.
Input

The input will be a single line containing an integer

N. Note: The input number will not be greater than 1010.
Output

The output should be a single line containing the representation of the English words of number

N. See the sample input and output for the English words of numbers in places.
Explanation

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




1
Expert's answer
2021-05-17T09:56:17-0400
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS