Search This Blog

Saturday, October 22, 2016

Convert Number into words using recursion


Recursion:  When a function calls itself again and again the process called recursion. The function which is being call is known as recursive function, and the process is called recursion.

Recently, I've a requirement in my project to print amount (number) into words like screenshot given below.



So I've implemented the following code. Below function is applicable till 999999999.
private static string ConvertIntoWords(long number)
       {
           if (number == 0)
               return "zero";
           if (number < 0)
               return "minus " + ConvertIntoWords(Math.Abs(number));
           string words = "";
           if ((number / 10000000) > 0)
           {
               words += ConvertIntoWords(number / 10000000) + " crores ";
               number %= 10000000;
           }
           if ((number / 100000) > 0)
           {
               words += ConvertIntoWords(number / 100000) + " lacs ";
               number %= 100000;
           }
           if ((number / 1000) > 0)
           {
               words += ConvertIntoWords(number / 1000) + " thousand ";
               number %= 1000;
           }
           if ((number / 100) > 0)
           {
               words += ConvertIntoWords(number / 100) + " hundred ";
               number %= 100;
           }
           if (number > 0)
           {
               var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
               var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
               if (number < 20)
                   words += unitsMap[number];
               else
               {
                   words += tensMap[number / 10];
                   if ((number % 10) > 0)
                       words += " "+unitsMap[number % 10];
                       //words += "-" + unitsMap[number % 10];
               }
           }
           return words;
       }
The above function recursively calls itself  and I've taken two arrays unitsMap and tensMap to find corresponding word to a digit.

Calling above function in C# Console Application:

First check whether entered string is valid number or not. If string is a valid number make a call to ConvertIntoWords() function to get the words representation of number otherwise ask user to enter a valid number.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberToWords
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Please enter number");
           long num;
           if(long.TryParse(Console.ReadLine(), out num)) //whether number is valid or not
           {
               Console.WriteLine("You entered : " + ConvertIntoWords(num));
               Console.ReadLine();
           }
           else
           {
               Console.WriteLine("Please enter valid number");
               Console.ReadLine();
           }
       }
}

Example Flow:









No comments:

Post a Comment