Computer Science, asked by jayitrinayak, 5 months ago

Write a program to input a natural number less than 1000 and display it in words.
Test your program for the given sample data and some random data.

INPUT:
29
OUTPUT:
TWENTY NINE
INPUT:
17001
OUTPUT:
OUT OF RANGE
INPUT:
119
OUTPUT:
ONE HUNDRED AND NINETEEN
INPUT:
500
OUTPUT:
FIVE HUNDRED

Answers

Answered by NainaRamroop
0

Python Program:

print("Enter the number")

num = input()

l = len(num)

if (l == 0):

   print("empty string")

if (l > 4):

   print("Length more than 4 is not supported")

   exit(-1)

single_digit = ["ZERO", "ONE", "TWO", "THREE","FOUR", "FIVE", "SIX", "SEVEN","EIGHT", "NINE"]

two_digits = ["", "TEN", "ELEVEN", "TWELVE","THIRTEEN", "FOURTEEN", "FIFTEEN","SIXTEEN", "SEVENTEEN", "EIGHTEEN","NINETEEN"]

tens_multiple = ["", "", "TWENTY", "THIRTY", "FORTY","FIFTY", "SIXTY", "SEVENTY", "EIGHTY","NINETY"]

tens_power = ["HUNDRED", "THOUSAND"]

print(num, ":", end=" ")

if (l == 1):

   print(single_digit[ord(num[0]) - 48])

x = 0

while (x < len(num)):

   if (l >= 3):

    if (ord(num[x]) - 48 != 0):

     print(single_digit[ord(num[x]) - 48],

      end=" ")

     print(tens_power[l - 3], end=" ")

    l -= 1

   else:

    if (ord(num[x]) - 48 == 1):

     sum = (ord(num[x]) - 48 +

      ord(num[x+1]) - 48)

     print(two_digits[sum])

    elif (ord(num[x]) - 48 == 2 and

     ord(num[x + 1]) - 48 == 0):

     print("twenty")

       else:

     i = ord(num[x]) - 48

     if(i > 0):

      print(tens_multiple[i], end=" ")

     else:

      print("", end="")

     x += 1

     if(ord(num[x]) - 48 != 0):

      print(single_digit[ord(num[x]) - 48])

   x += 1

Output:

Enter the number

29

29 : TWENTY NINE

Enter the number

17001

OUT OF RANGE

Enter the number

199

199 : ONE HUNDRED NINETY NINE

Enter the number

500

500 : FIVE HUNDRED

#SPJ1

Similar questions