{"id":25723,"date":"2021-12-04T09:19:46","date_gmt":"2021-12-04T03:49:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=25723"},"modified":"2021-12-04T09:19:46","modified_gmt":"2021-12-04T03:49:46","slug":"python-program-to-convert-a-number-to-words-digit-by-digit","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-a-number-to-words-digit-by-digit\/","title":{"rendered":"Python Program to Convert a Number to Words (digit by digit)"},"content":{"rendered":"
Given a number and the task is to convert the given number into words.<\/p>\n
For Example:<\/strong><\/p>\n Let number = 15<\/p>\n The output\u00a0 will be = “one-five”<\/p>\n Implementation Tips:<\/strong><\/p>\n Make a global list with words for each digit from 0 to 9. The entries in the list will be mapped to the index, as shown in the table below.<\/p>\n global list = [ ‘zero’,\u00a0 ‘one’, ……………’Nine’]<\/p>\n index:\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 1 ……………..\u00a0 \u00a0 \u00a09<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Given a number and the task is to convert the given number into words. For Example: Let number = 15 The output\u00a0 will be = “one-five” Implementation Tips: Make a global list with words for each digit from 0 to 9. The entries in the list will be mapped to the index, as shown in …<\/p>\nGiven number = 15<\/pre>\n
The conversion of given number{ 15 } into words = \r\none five<\/pre>\n
Given number = 678<\/pre>\n
The conversion of given number{ 678 } into words = \r\nsix seven eight<\/pre>\n
Program to Convert a Number to Words (digit by digit) in Python<\/h2>\n
\n
Method #1: Using Recursion (Static Input)<\/h3>\n
\n
# Give the global list with words for each digit from 0 to 9 and\r\n# store it in another variable.\r\nglobl_lst = ['zero', 'one', 'two', 'three', 'four',\r\n 'five', 'six', 'seven', 'eight', 'nine']\r\n\r\n# Create a function say convert_numbrtoword() which accepts the given number as\r\n# an argument and returns the conversion of the given number into words.\r\n\r\n\r\ndef convert_numbrtoword(gvn_num):\r\n\r\n # Inside the function, check if the given number is equal to 0 using the if\r\n # conditional statement.\r\n if(gvn_num == 0):\r\n # If it is true, then return an empty string.\r\n return \"\"\r\n\r\n else:\r\n # Else get the spelling for the last digit by giving the index of the global\r\n # list as given number modulus 10 which gives the last digit of the given\r\n # number.\r\n # Store it in a variable.\r\n rslt = globl_lst[gvn_num % 10]\r\n\r\n # Add the above result and call the convert_numbrtoword() function itself by\r\n # dividing the number by 10 (Recursive Logic).\r\n # Store it in another variable.\r\n fnl_rslt = convert_numbrtoword(int(gvn_num\/10)) + rslt + \" \"\r\n\r\n # Return the above result.\r\n return fnl_rslt\r\n\r\n\r\n# Give the number as static input and store it in a variable.\r\ngvn_num = 15\r\nprint(\"The conversion of given number{\", gvn_num, \"} into words = \")\r\n# Pass the given number as an argument to the above convert_numbrtoword()\r\n# function and print it.\r\nprint(convert_numbrtoword(gvn_num))\r\n<\/pre>\n
The conversion of given number{ 15 } into words = \r\none five<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Give the global list with words for each digit from 0 to 9 and\r\n# store it in another variable.\r\nglobl_lst = ['zero', 'one', 'two', 'three', 'four',\r\n 'five', 'six', 'seven', 'eight', 'nine']\r\n\r\n# Create a function say convert_numbrtoword() which accepts the given number as\r\n# an argument and returns the conversion of the given number into words.\r\n\r\n\r\ndef convert_numbrtoword(gvn_num):\r\n\r\n # Inside the function, check if the given number is equal to 0 using the if\r\n # conditional statement.\r\n if(gvn_num == 0):\r\n # If it is true, then return an empty string.\r\n return \"\"\r\n\r\n else:\r\n # Else get the spelling for the last digit by giving the index of the global\r\n # list as given number modulus 10 which gives the last digit of the given\r\n # number.\r\n # Store it in a variable.\r\n rslt = globl_lst[gvn_num % 10]\r\n\r\n # Add the above result and call the convert_numbrtoword() function itself by\r\n # dividing the number by 10 (Recursive Logic).\r\n # Store it in another variable.\r\n fnl_rslt = convert_numbrtoword(int(gvn_num\/10)) + rslt + \" \"\r\n\r\n # Return the above result.\r\n return fnl_rslt\r\n\r\n\r\n# Give the number as user input using the int(input()) function.\r\n# store it in a variable.\r\ngvn_num = int(input(\"Enter some random number = \"))\r\nprint(\"The conversion of given number{\", gvn_num, \"} into words = \")\r\n# Pass the given number as an argument to the above convert_numbrtoword()\r\n# function and print it.\r\nprint(convert_numbrtoword(gvn_num))\r\n<\/pre>\n
Enter some random number = 678\r\nThe conversion of given number{ 678 } into words = \r\nsix seven eight<\/pre>\n","protected":false},"excerpt":{"rendered":"