Program to convert a number in words.| Python


Write a program to convert a number entered by the user into its corresponding number in words. For example, if the input is 876 then the output should be ‘Eight Seven Six’.


Testcases1:


Input:
234


Output:
two three four


Testcases2:
Input:
709
Output:
seven zero nine

Steps:

1.Let understand the program the use input() function to input the number and take empty list to append digits of that number separately and another list for input number in alphabetical form.

2.Then get the digits of given number and then append digits in list i.e L.

3.Now list is formed then take a loop which will go from last element to first element of the list because the digit append in the list in opposite order example:234--->[4,3,2]

4.Then we test the every element of the list if the condition is true then append that alphabetical name .

5.In the last print that list in string form using explicit by str() function.

"""
         Write a function to convert a number entered by the user into its corresponding number in words. For example, if the input is 876 then the output should be ‘Eight Seven Six’.
         
         
         n=123
         output
         one two three
         
         n=234
         two three four
         
         """
         #Let read the quetsion then now input number using int() function
         n=int(input("Enter a number:"))
         #Then we take empty list to input the number 
         L=[ ]
         #This is another empty list for appending in real form in alphabetic form
         List=[]
         
         #Take a while until number is greater than 0 till it will run
         while(n>0):
             rem=n%10       #rem=7899%10=9
             L.append(rem)  #L=[9],..................
             n=n//10        #n=7899//10=789
         print(L)           #L=[9,9,8,7]
         m=len(L)
         
         #because list in descending it means it is in opposite form that's why we take a for loop from ending element of the  list
         for i in range(m-1,-1,-1):
         
         #Then we test every element of the list and append whatever you have given argument if the condition is true using if elif else structure
             if L[i]==1:
                 List.append("one")
             elif L[i]==2:
                 List.append("Two")
             elif L[i]==3:
                 List.append("Three")
             elif L[i]==4:
                 List.append("Four")
             elif L[i]==5:
                 List.append("Five")
             elif L[i]==6:
                 List.append("Six")
             elif L[i]==7:
                 List.append("Seven")
             elif L[i]==8:
                 List.append("Eight")
             elif L[i]==9:
                 List.append("Nine")
             else:
                 List.append("Zero")
         
         #At the last use print() function to print List in string form using str() function
         # now convert in spelling we take s variable that is space

         s=" "
# then take for loop and every element concatenate in spelling form for j in range(0,m): s=s+List[j] print(s)
         























































































No comments:

Powered by Blogger.