def number_to_words(num):
if num == 0:
return "Zero"
# Store the word representations of the numbers from 1 to 19
below_20 = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
# Store the word representations of tens numbers
tens = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
# Store the word representations of thousands numbers
thousands = ["", "Thousand", "Million", "Billion"]
# Function to find word representation of a three-digit number
def represent(n):
if n == 0:
return ""
elif n < 20:
return below_20[n-1] + " "
elif n < 100:
return tens[n // 10 - 2] + " " + represent(n % 10)
else:
return below_20[n // 100 - 1] + " Hundred " + represent(n % 100)
# Initialize the result string
result = ""
# For each string in thousands
for t in thousands:
# If the last three digits are not zero
if num % 1000 != 0:
# Get the representation of the three-digit number, append result to it, and
# store it back in result
result = represent(num % 1000) + t + " " + result
# Divide num by 1000 and update num with the quotient
num //= 1000
# Return the result string
return result.strip()
def main():
nums = [0, 1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890]
for i in range(len(nums)):
print(i + 1, ".\tnum: ", nums[i], sep = "")
print("\tWords:", number_to_words(nums[i]))
print("-"*100)
if __name__ == "__main__":
main()