Posted on by Kalkicode
Code Conversion

# Convert number to words

The task is to implement a program that takes a numerical input and converts it into its word representation. The input can be any positive integer up to 19 digits long. The program should be able to handle numbers in the range from 0 to 999,999,999,999,999,999 (nineteen nines).

For instance, if the input is "12345678", the program should output "One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight".

## Algorithm

To convert a number into words, we can follow the following algorithm:

1. Define arrays to store the word representation of digits, currency names, and multiples of ten.
2. Write a recursive function to handle the translation of a given part of the number.
3. In the main function, iterate through the digits of the number.
4. If the digit is not zero, determine the appropriate translation based on the number of digits remaining and print the corresponding word.
5. Continue this process until all digits have been processed.

## Pseudocode

``````function translate(num, length, start):
if length > 0:
if length = 1:
ans = num[start] - '0'
print simple_digit[ans]
translate(num, length - 1, start + 1)
else if length = 2:
if num[start] = '0':
translate(num, length - 1, start + 1)
else:
ans = (num[start] - '0') * 10 + (num[start + 1] - '0')
if ans >= 10 and ans <= 19:
print simple_digit[ans]
else:
ans = num[start] - '0'
print multiplex[ans]
translate(num, length - 1, start + 1)

function printWord(num, length):
if length = 1:
translate(num, 1, 0)
return
counter = 0
digits = length
status = 0
while counter < length and digits > 0:
if num[counter] != '0':
status = 1
if digits = 1:
translate(num, 1, counter)
counter += 1
digits -= 1
else if digits = 2:
translate(num, 2, counter)
counter += 2
digits -= 2
else:
if digits = 3 or digits % 2 = 0:
translate(num, 1, counter)
counter += 1
digits -= 1
print currency[digits]
else:
translate(num, 2, counter)
counter += 2
digits -= 2
print currency[digits]
else:
counter++
digits--
if status = 0:
print "Zero"

function numToWords(num):
length = length(num)
print "Given Number: [" + num + "]"
if length > 19:
print "Number Out of range"
return
printWord(num, length)
```
```

Converting a number to words means representing a numerical value using words instead of numerals. For example, the number "123" can be converted to "one hundred and twenty-three" in words. This conversion can be useful in various contexts, such as in financial documents, legal documents, or when writing out a check. It can also be helpful for individuals who have difficulty reading or understanding numbers. The process of converting numbers to words involves determining the value of each digit and then expressing that value in words based on a set of rules for constructing the word form.

## Program Solution

``````// C program
// Convert number to words
#include <stdio.h>
#include <string.h>
/*
You can change exist currency by country
Example
// International System
const char *const currency[] =  {"","","Hundred","Thousand","Ten Thousand", "Hundred Thousand","Million","Ten Million","Hundred Million","Billon" ,"Ten Billion","Hundred Billion","Trillions","Ten Trillions" , "Hundred Trillions","Quadrillion","Ten Quadrillion","Hundred Quadrillion","Quintillion","Ten Quintillion"}

*/
// Here mentioning indian currency
const char *const currency[] = {
"" , "" , "Hundred" , "Thousand" , "Ten Thousand" , "Lakh" , "Ten Lakh" , "Crore" , "Ten Crore" , "Arab" , "Ten Arab" , "Kharab" , "Ten Kharab" , "Neel" , "Ten Neel" , "Padma" , "Ten Padma" , "Shankh" , "Ten Shankh" , "Maha-Shankh"
};
// Define name of 0...19
const char *const simple_digit[] = {
"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen"
};
// Define name of 20,30,40,50,60,70,80,90
const char *const multiplex[] = {
"" , "" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sity" , "Seventy" , "Eighty" , "Ninety"
};
void translate(char *num, int length, int start)
{
if (length > 0)
{
int ans = 0;
if (length == 1)
{
ans = num[start] - '0';
printf(" %s", simple_digit[ans]);
translate(num, length - 1, start + 1);
}
else if (length == 2)
{
if (num[start] == '0')
{
translate(num, length - 1, start + 1);
}
else
{
ans = num[start] - '0';
ans *= 10 + num[start + 1] - '0';
if (ans >= 10 && ans <= 19)
{
printf(" %s", simple_digit[ans]);
}
else
{
ans = num[start] - '0';
printf(" %s", multiplex[ans]);
translate(num, length - 1, start + 1);
}
}
}
}
}
void printWord(char *num, int length)
{
if (length == 1)
{
// When number is single digit
translate(num, 1, 0);
return;
}
int counter = 0;
int digits = length;
int status = 0;
while (counter < length && digits > 0)
{
if (num[counter] != '0')
{
// Tracker to find result
status = 1;
if (digits == 1)
{
// When work with single digit
translate(num, 1, counter);
counter += 1;
digits -= 1;
}
else if (digits == 2)
{
// When work with two digit
translate(num, 2, counter);
counter += 2;
digits -= 2;
}
else
{
// When number digit length more than 2
// Case A
if (digits == 3 || digits % 2 == 0)
{
// When work with single digit
translate(num, 1, counter);
counter += 1;
digits -= 1;
printf(" %s", currency[digits]);
}
else
{
translate(num, 2, counter);
counter += 2;
digits -= 2;
printf(" %s", currency[digits]);
}
}
}
else
{
counter++;
digits--;
}
}
if (status == 0)
{
// When In case get the all zero
printf(" Zero ");
}
}
// Handles the request to print number words
void numToWords(char *num)
{
// Get the length
int length = strlen(num);
printf(" Given Number : [%s]\n", num);
if (length > 19)
{
printf(" Number Out of range \n");
return;
}
// We assuming that given number is valid number
// And it is positive
printWord(num, length);
printf("\n");
}
int main()
{
numToWords("123");
numToWords("12345678");
numToWords("8905463");
numToWords("100000000");
numToWords("1000100001");
numToWords("98815283");
numToWords("842321");
numToWords("5125235677842321");
numToWords("9999");
numToWords("86");
}``````

#### Output

`````` Given Number : [123]
One Hundred Twenty Three
Given Number : [12345678]
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
Given Number : [8905463]
Eighty Nine Lakh Five Thousand Four Hundred Sity Three
Given Number : [100000000]
Ten Crore
Given Number : [1000100001]
One Arab One Lakh One
Given Number : [98815283]
Nine Crore Eighty Eight Lakh Fifteen Thousand Two Hundred Eighty Three
Given Number : [842321]
Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [5125235677842321]
Five Padma Twelve Neel Fifty Two Kharab Thirty Five Arab Sity Seven Crore Seventy Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [9999]
Nine Thousand Nine Hundred Ninety Nine
Given Number : [86]
Eighty Six``````
``````/*
Java Program
Convert number to words
*/
public class NumberToWords
{
/*
You can change exist currency by country
Example
// International System
const char * const currency[] =  {"","","Hundred","Thousand","Ten Thousand", "Hundred Thousand","Million","Ten Million","Hundred Million","Billion" ,"Ten Billion","Hundred Billion","Trillions","Ten Trillions" , "Hundred Trillions","Quadrillion","Ten Quadrillion","Hundred Quadrillion","Quintillion","Ten Quintillion"}

*/
// Here mentioning indian currency
String[] currency = {
"" , "" , "Hundred" , "Thousand" , "Ten Thousand" , "Lakh" , "Ten Lakh" , "Crore" , "Ten Crore" , "Arab" , "Ten Arab" , "Kharab" , "Ten Kharab" , "Neel" , "Ten Neel" , "Padma" , "Ten Padma" , "Shankh" , "Ten Shankh" , "Maha-Shankh"
};
// Define name of 0...19
String[] simpleDigit = {
"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen"
};
// Define name of 20,30,40,50,60,70,80,90
String[] multiplex = {
"" , "" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sity" , "Seventy" , "Eighty" , "Ninety"
};
public void translate(String num, int length, int start)
{
if (length > 0)
{
int ans = 0;
if (length == 1)
{
ans = num.charAt(start) - '0';
System.out.print(" " + simpleDigit[ans]);
translate(num, length - 1, start + 1);
}
else if (length == 2)
{
if (num.charAt(start) == '0')
{
translate(num, length - 1, start + 1);
}
else
{
ans = num.charAt(start) - '0';
ans *= 10 + num.charAt(start + 1) - '0';
if (ans >= 10 && ans <= 19)
{
System.out.print(" " + simpleDigit[ans]);
}
else
{
ans = num.charAt(start) - '0';
System.out.print(" " + multiplex[ans]);
translate(num, length - 1, start + 1);
}
}
}
}
}
public void printWord(String num, int length)
{
if (length == 1)
{
// When number is single digit
translate(num, 1, 0);
return;
}
int counter = 0;
int digits = length;
boolean status = false;
while (counter < length && digits > 0)
{
if (num.charAt(counter) != '0')
{
// Tracker to find result
status = true;
if (digits == 1)
{
// When work with single digit
translate(num, 1, counter);
counter += 1;
digits -= 1;
}
else if (digits == 2)
{
// When work with two digit
translate(num, 2, counter);
counter += 2;
digits -= 2;
}
else
{
// When number digit length more than 2
// Case A
if (digits == 3 || digits % 2 == 0)
{
// When work with single digit
translate(num, 1, counter);
counter += 1;
digits -= 1;
System.out.print(" " + currency[digits]);
}
else
{
translate(num, 2, counter);
counter += 2;
digits -= 2;
System.out.print(" " + currency[digits]);
}
}
}
else
{
counter++;
digits--;
}
}
if (status == false)
{
// When In case get the all zero
System.out.print(" Zero ");
}
}
// Handles the request to print number words
public void words(String num)
{
// Get the length
int length = num.length();
System.out.print(" Given Number : [" + num + "]\n");
if (length > 19)
{
System.out.print(" Number Out of range \n");
return;
}
// We assuming that given number is valid number
// And it is positive
printWord(num, length);
System.out.print("\n");
}
public static void main(String args[])
{
// Test Case
}
}``````

#### Output

`````` Given Number : [123]
One Hundred Twenty Three
Given Number : [12345678]
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
Given Number : [8905463]
Eighty Nine Lakh Five Thousand Four Hundred Sity Three
Given Number : [100000000]
Ten Crore
Given Number : [1000100001]
One Arab One Lakh One
Given Number : [98815283]
Nine Crore Eighty Eight Lakh Fifteen Thousand Two Hundred Eighty Three
Given Number : [842321]
Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [5125235677842321]
Five Padma Twelve Neel Fifty Two Kharab Thirty Five Arab Sity Seven Crore Seventy Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [9999]
Nine Thousand Nine Hundred Ninety Nine
Given Number : [86]
Eighty Six``````
``````// Include header file
#include <iostream>

#include<string.h>

using namespace std;
/*
C++ Program
Convert number to words
*/
class NumberToWords
{
public:
/*
You can change exist currency by country
Example
// International System
const char *const currency[] =  {"","","Hundred","Thousand","Ten Thousand", "Hundred Thousand","Million","Ten Million","Hundred Million","Billion" ,"Ten Billion","Hundred Billion","Trillions","Ten Trillions" , "Hundred Trillions","Quadrillion","Ten Quadrillion","Hundred Quadrillion","Quintillion","Ten Quintillion"}

*/
// Here mentioning indian currency
const char *const currency[20] = {
"" , "" , "Hundred" , "Thousand" , "Ten Thousand" , "Lakh" , "Ten Lakh" , "Crore" , "Ten Crore" , "Arab" , "Ten Arab" , "Kharab" , "Ten Kharab" , "Neel" , "Ten Neel" , "Padma" , "Ten Padma" , "Shankh" , "Ten Shankh" , "Maha-Shankh"
};
// Define name of 0...19
const char *const simpleDigit[20] = {
"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen"
};
// Define name of 20,30,40,50,60,70,80,90
const char *const multiplex[10] = {
"" , "" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sity" , "Seventy" , "Eighty" , "Ninety"
};
void translate(string num, int length, int start)
{
if (length > 0)
{
int ans = 0;
if (length == 1)
{
ans = num[start] - '0';
cout << " " << this->simpleDigit[ans];
this->translate(num, length - 1, start + 1);
}
else if (length == 2)
{
if (num[start] == '0')
{
this->translate(num, length - 1, start + 1);
}
else
{
ans = num[start] - '0';
ans *= 10 + num[start + 1] - '0';
if (ans >= 10 && ans <= 19)
{
cout << " " << this->simpleDigit[ans];
}
else
{
ans = num[start] - '0';
cout << " " << this->multiplex[ans];
this->translate(num, length - 1, start + 1);
}
}
}
}
}
void printWord(string num, int length)
{
if (length == 1)
{
// When number is single digit
this->translate(num, 1, 0);
return;
}
int counter = 0;
int digits = length;
bool status = false;
while (counter < length && digits > 0)
{
if (num[counter] != '0')
{
// Tracker to find result
status = true;
if (digits == 1)
{
// When work with single digit
this->translate(num, 1, counter);
counter += 1;
digits -= 1;
}
else if (digits == 2)
{
// When work with two digit
this->translate(num, 2, counter);
counter += 2;
digits -= 2;
}
else
{
// When number digit length more than 2
// Case A
if (digits == 3 || digits % 2 == 0)
{
// When work with single digit
this->translate(num, 1, counter);
counter += 1;
digits -= 1;
cout << " " << this->currency[digits];
}
else
{
this->translate(num, 2, counter);
counter += 2;
digits -= 2;
cout << " " << this->currency[digits];
}
}
}
else
{
counter++;
digits--;
}
}
if (status == false)
{
// When In case get the all zero
cout << " Zero ";
}
}
// Handles the request to print number words
void words(string num)
{
// Get the length
int length = num.size();
cout << " Given Number : [" << num << "]\n";
if (length > 19)
{
cout << " Number Out of range \n";
return;
}
// We assuming that given number is valid number
// And it is positive
this->printWord(num, length);
cout << "\n";
}
};
int main()
{
// Test Case
return 0;
}``````

#### Output

`````` Given Number : [123]
One Hundred Twenty Three
Given Number : [12345678]
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
Given Number : [8905463]
Eighty Nine Lakh Five Thousand Four Hundred Sity Three
Given Number : [100000000]
Ten Crore
Given Number : [1000100001]
One Arab One Lakh One
Given Number : [98815283]
Nine Crore Eighty Eight Lakh Fifteen Thousand Two Hundred Eighty Three
Given Number : [842321]
Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [5125235677842321]
Five Padma Twelve Neel Fifty Two Kharab Thirty Five Arab Sity Seven Crore Seventy Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [9999]
Nine Thousand Nine Hundred Ninety Nine
Given Number : [86]
Eighty Six``````
``````// Include namespace system
using System;
/*
C# Program
Convert number to words
*/
public class NumberToWords
{
String[] currency;
String[] simpleDigit;
String[] multiplex;
public NumberToWords()
{
// Allocate memory
this.currency = new String[20];
this.simpleDigit = new String[20];
this.multiplex = new String[10];
this.getDefault();
}
private void getDefault()
{
// Define name of 20,30,40,50,60,70,80,90
multiplex[0] = "";
multiplex[1] = "";
multiplex[2] = "Twenty";
multiplex[3] = "Thirty";
multiplex[4] = "Forty";
multiplex[5] = "Fifty";
multiplex[6] = "Sity";
multiplex[7] = "Seventy";
multiplex[8] = "Eighty";
multiplex[9] = "Ninety";
// Define name of 0...19
simpleDigit[0] = "Zero";
simpleDigit[1] = "One";
simpleDigit[2] = "Two";
simpleDigit[3] = "Three";
simpleDigit[4] = "Four";
simpleDigit[5] = "Five";
simpleDigit[6] = "Six";
simpleDigit[7] = "Seven";
simpleDigit[8] = "Eight";
simpleDigit[9] = "Nine";
simpleDigit[10] = "Ten";
simpleDigit[11] = "Eleven";
simpleDigit[12] = "Twelve";
simpleDigit[13] = "Thirteen";
simpleDigit[14] = "Fourteen";
simpleDigit[15] = "Fifteen";
simpleDigit[16] = "Sixteen";
simpleDigit[17] = "Seventeen";
simpleDigit[18] = "Eighteen";
simpleDigit[19] = "Nineteen";
/*
You can change exist currency by country
Example
// International System
currency[0]  = "";
currency[1]  = "";
currency[2]  = "Hundred";
currency[3]  = "Thousand";
currency[4]  = "Ten Thousand";
currency[5]  = "Hundred Thousand";
currency[6]  = "Million";
currency[7]  = "Ten Million";
currency[8]  = "Hundred Million";
currency[9]  = "Billion";
currency[10] = "Ten Billion";
currency[11] = "Hundred Billion";
currency[12] = "Trillion";
currency[13] = "Ten Trillion ";
currency[14] = "Hundred Trillion";
currency[18] = "Quintillion";
currency[19] = "Ten Quintillion";
*/
// Here mentioning india currency
currency[0] = "";
currency[1] = "";
currency[2] = "Hundred";
currency[3] = "Thousand";
currency[4] = "Ten Thousand";
currency[5] = "Lakh";
currency[6] = "Ten Lakh";
currency[7] = "Crore";
currency[8] = "Ten Crore";
currency[9] = "Arab";
currency[10] = "Ten Arab";
currency[11] = "Kharab";
currency[12] = "Ten Kharab";
currency[13] = "Neel";
currency[14] = "Ten Neel";
currency[17] = "Shankh";
currency[18] = "Ten Shankh";
currency[19] = "Maha-Shankh";
}
public void translate(String num, int length, int start)
{
if (length > 0)
{
int ans = 0;
if (length == 1)
{
ans = num[start] - '0';
Console.Write(" " + simpleDigit[ans]);
translate(num, length - 1, start + 1);
}
else if (length == 2)
{
if (num[start] == '0')
{
translate(num, length - 1, start + 1);
}
else
{
ans = num[start] - '0';
ans *= 10 + num[start + 1] - '0';
if (ans >= 10 && ans <= 19)
{
Console.Write(" " + simpleDigit[ans]);
}
else
{
ans = num[start] - '0';
Console.Write(" " + multiplex[ans]);
translate(num, length - 1, start + 1);
}
}
}
}
}
public void printWord(String num, int length)
{
if (length == 1)
{
// When number is single digit
translate(num, 1, 0);
return;
}
int counter = 0;
int digits = length;
Boolean status = false;
while (counter < length && digits > 0)
{
if (num[counter] != '0')
{
// Tracker to find result
status = true;
if (digits == 1)
{
// When work with single digit
translate(num, 1, counter);
counter += 1;
digits -= 1;
}
else if (digits == 2)
{
// When work with two digit
translate(num, 2, counter);
counter += 2;
digits -= 2;
}
else
{
// When number digit length more than 2
// Case A
if (digits == 3 || digits % 2 == 0)
{
// When work with single digit
translate(num, 1, counter);
counter += 1;
digits -= 1;
Console.Write(" " + currency[digits]);
}
else
{
translate(num, 2, counter);
counter += 2;
digits -= 2;
Console.Write(" " + currency[digits]);
}
}
}
else
{
counter++;
digits--;
}
}
if (status == false)
{
// When In case get the all zero
Console.Write(" Zero ");
}
}
// Handles the request to print number words
public void words(String num)
{
// Get the length
int length = num.Length;
Console.Write(" Given Number : [" + num + "]\n");
if (length > 19)
{
Console.Write(" Number Out of range \n");
return;
}
// We assuming that given number is valid number
// And it is positive
printWord(num, length);
Console.Write("\n");
}
public static void Main(String []args)
{
// Test Case
}
}``````

#### Output

`````` Given Number : [123]
One Hundred Twenty Three
Given Number : [12345678]
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
Given Number : [8905463]
Eighty Nine Lakh Five Thousand Four Hundred Sity Three
Given Number : [100000000]
Ten Crore
Given Number : [1000100001]
One Arab One Lakh One
Given Number : [98815283]
Nine Crore Eighty Eight Lakh Fifteen Thousand Two Hundred Eighty Three
Given Number : [842321]
Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [5125235677842321]
Five Padma Twelve Neel Fifty Two Kharab Thirty Five Arab Sity Seven Crore Seventy Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [9999]
Nine Thousand Nine Hundred Ninety Nine
Given Number : [86]
Eighty Six``````
``````<?php
/*
Php Program
Convert number to words
*/
class NumberToWords
{
public \$currency;
public \$simpleDigit;
public \$multiplex;

function __construct()
{
// Allocate memory
\$this->currency = array_fill(0, 20, "");
\$this->simpleDigit = array_fill(0, 20, "");
\$this->multiplex = array_fill(0, 10, "");
\$this->getDefault();
}
private
function getDefault()
{
// Define name of 20,30,40,50,60,70,80,90
\$this->multiplex[0] = "";
\$this->multiplex[1] = "";
\$this->multiplex[2] = "Twenty";
\$this->multiplex[3] = "Thirty";
\$this->multiplex[4] = "Forty";
\$this->multiplex[5] = "Fifty";
\$this->multiplex[6] = "Sity";
\$this->multiplex[7] = "Seventy";
\$this->multiplex[8] = "Eighty";
\$this->multiplex[9] = "Ninety";
// Define name of 0...19
\$this->simpleDigit[0] = "Zero";
\$this->simpleDigit[1] = "One";
\$this->simpleDigit[2] = "Two";
\$this->simpleDigit[3] = "Three";
\$this->simpleDigit[4] = "Four";
\$this->simpleDigit[5] = "Five";
\$this->simpleDigit[6] = "Six";
\$this->simpleDigit[7] = "Seven";
\$this->simpleDigit[8] = "Eight";
\$this->simpleDigit[9] = "Nine";
\$this->simpleDigit[10] = "Ten";
\$this->simpleDigit[11] = "Eleven";
\$this->simpleDigit[12] = "Twelve";
\$this->simpleDigit[13] = "Thirteen";
\$this->simpleDigit[14] = "Fourteen";
\$this->simpleDigit[15] = "Fifteen";
\$this->simpleDigit[16] = "Sixteen";
\$this->simpleDigit[17] = "Seventeen";
\$this->simpleDigit[18] = "Eighteen";
\$this->simpleDigit[19] = "Nineteen";
/*
You can change exist currency by country
Example
// International System
currency[0]  = "";
currency[1]  = "";
currency[2]  = "Hundred";
currency[3]  = "Thousand";
currency[4]  = "Ten Thousand";
currency[5]  = "Hundred Thousand";
currency[6]  = "Million";
currency[7]  = "Ten Million";
currency[8]  = "Hundred Million";
currency[9]  = "Billion";
currency[10] = "Ten Billion";
currency[11] = "Hundred Billion";
currency[12] = "Trillion";
currency[13] = "Ten Trillion ";
currency[14] = "Hundred Trillion";
currency[18] = "Quintillion";
currency[19] = "Ten Quintillion";
*/
// Here mentioning indian currency
\$this->currency[0] = "";
\$this->currency[1] = "";
\$this->currency[2] = "Hundred";
\$this->currency[3] = "Thousand";
\$this->currency[4] = "Ten Thousand";
\$this->currency[5] = "Lakh";
\$this->currency[6] = "Ten Lakh";
\$this->currency[7] = "Crore";
\$this->currency[8] = "Ten Crore";
\$this->currency[9] = "Arab";
\$this->currency[10] = "Ten Arab";
\$this->currency[11] = "Kharab";
\$this->currency[12] = "Ten Kharab";
\$this->currency[13] = "Neel";
\$this->currency[14] = "Ten Neel";
\$this->currency[17] = "Shankh";
\$this->currency[18] = "Ten Shankh";
\$this->currency[19] = "Maha-Shankh";
}
public  function translate(\$num, \$length, \$start)
{
if (\$length > 0)
{
\$ans = 0;
if (\$length == 1)
{
\$ans = ord(\$num[\$start]) - ord('0');
echo " ". \$this->simpleDigit[\$ans];
\$this->translate(\$num, \$length - 1, \$start + 1);
}
else if (\$length == 2)
{
if (\$num[\$start] == '0')
{
\$this->translate(\$num, \$length - 1, \$start + 1);
}
else
{
\$ans = ord(\$num[\$start]) - ord('0');
\$ans *= 10 + \$num[\$start + 1] - ord('0');
if (\$ans >= 10 && \$ans <= 19)
{
echo " ". \$this->simpleDigit[\$ans];
}
else
{
\$ans = ord(\$num[\$start]) - ord('0');
echo " ". \$this->multiplex[\$ans];
\$this->translate(\$num, \$length - 1, \$start + 1);
}
}
}
}
}
public  function printWord(\$num, \$length)
{
if (\$length == 1)
{
// When number is single digit
\$this->translate(\$num, 1, 0);
return;
}
\$counter = 0;
\$digits = \$length;
\$status = false;
while (\$counter < \$length && \$digits > 0)
{
if (\$num[\$counter] != '0')
{
// Tracker to find result
\$status = true;
if (\$digits == 1)
{
// When work with single digit
\$this->translate(\$num, 1, \$counter);
\$counter += 1;
\$digits -= 1;
}
else if (\$digits == 2)
{
// When work with two digit
\$this->translate(\$num, 2, \$counter);
\$counter += 2;
\$digits -= 2;
}
else
{
// When number digit length more than 2
// Case A
if (\$digits == 3 || \$digits % 2 == 0)
{
// When work with single digit
\$this->translate(\$num, 1, \$counter);
\$counter += 1;
\$digits -= 1;
echo " ". \$this->currency[\$digits];
}
else
{
\$this->translate(\$num, 2, \$counter);
\$counter += 2;
\$digits -= 2;
echo " ". \$this->currency[\$digits];
}
}
}
else
{
\$counter++;
\$digits--;
}
}
if (\$status == false)
{
// When In case get the all zero
echo " Zero ";
}
}
// Handles the request to print number words
public  function words(\$num)
{
// Get the length
\$length = strlen(\$num);
echo " Given Number : [". \$num ."]\n";
if (\$length > 19)
{
echo " Number Out of range \n";
return;
}
// We assuming that given number is valid number
// And it is positive
\$this->printWord(\$num, \$length);
echo "\n";
}
}

function main()
{
// Test Case
}
main();``````

#### Output

`````` Given Number : [123]
One Hundred Twenty Three
Given Number : [12345678]
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
Given Number : [8905463]
Eighty Nine Lakh Five Thousand Four Hundred Sity Three
Given Number : [100000000]
Zero Crore
Given Number : [1000100001]
One Arab One Lakh One
Given Number : [98815283]
Nine Crore Eighty Eight Lakh  Five Thousand Two Hundred Eighty Three
Given Number : [842321]
Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [5125235677842321]
Five Padma  Two Neel Fifty Two Kharab Thirty Five Arab Sity Seven Crore Seventy Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [9999]
Nine Thousand Nine Hundred Ninety Nine
Given Number : [86]
Eighty Six``````
``````/*
Node Js Program
Convert number to words
*/
class NumberToWords
{
constructor()
{
// Allocate memory
this.currency = Array(20).fill("");
this.simpleDigit = Array(20).fill("");
this.multiplex = Array(10).fill("");
this.getDefault();
}
getDefault()
{
// Define name of 20,30,40,50,60,70,80,90
this.multiplex[0] = "";
this.multiplex[1] = "";
this.multiplex[2] = "Twenty";
this.multiplex[3] = "Thirty";
this.multiplex[4] = "Forty";
this.multiplex[5] = "Fifty";
this.multiplex[6] = "Sity";
this.multiplex[7] = "Seventy";
this.multiplex[8] = "Eighty";
this.multiplex[9] = "Ninety";
// Define name of 0...19
this.simpleDigit[0] = "Zero";
this.simpleDigit[1] = "One";
this.simpleDigit[2] = "Two";
this.simpleDigit[3] = "Three";
this.simpleDigit[4] = "Four";
this.simpleDigit[5] = "Five";
this.simpleDigit[6] = "Six";
this.simpleDigit[7] = "Seven";
this.simpleDigit[8] = "Eight";
this.simpleDigit[9] = "Nine";
this.simpleDigit[10] = "Ten";
this.simpleDigit[11] = "Eleven";
this.simpleDigit[12] = "Twelve";
this.simpleDigit[13] = "Thirteen";
this.simpleDigit[14] = "Fourteen";
this.simpleDigit[15] = "Fifteen";
this.simpleDigit[16] = "Sixteen";
this.simpleDigit[17] = "Seventeen";
this.simpleDigit[18] = "Eighteen";
this.simpleDigit[19] = "Nineteen";
/*
You can change exist currency by country
Example
// International System
currency[0]  = "";
currency[1]  = "";
currency[2]  = "Hundred";
currency[3]  = "Thousand";
currency[4]  = "Ten Thousand";
currency[5]  = "Hundred Thousand";
currency[6]  = "Million";
currency[7]  = "Ten Million";
currency[8]  = "Hundred Million";
currency[9]  = "Billion";
currency[10] = "Ten Billion";
currency[11] = "Hundred Billion";
currency[12] = "Trillion";
currency[13] = "Ten Trillion ";
currency[14] = "Hundred Trillion";
currency[18] = "Quintillion";
currency[19] = "Ten Quintillion";
*/
// Here mentioning indian currency
this.currency[0] = "";
this.currency[1] = "";
this.currency[2] = "Hundred";
this.currency[3] = "Thousand";
this.currency[4] = "Ten Thousand";
this.currency[5] = "Lakh";
this.currency[6] = "Ten Lakh";
this.currency[7] = "Crore";
this.currency[8] = "Ten Crore";
this.currency[9] = "Arab";
this.currency[10] = "Ten Arab";
this.currency[11] = "Kharab";
this.currency[12] = "Ten Kharab";
this.currency[13] = "Neel";
this.currency[14] = "Ten Neel";
this.currency[17] = "Shankh";
this.currency[18] = "Ten Shankh";
this.currency[19] = "Maha-Shankh";
}
translate(num, length, start)
{
if (length > 0)
{
var ans = 0;
if (length == 1)
{
ans = (num[start]).charCodeAt(0) - ('0').charCodeAt(0);
process.stdout.write(" " + this.simpleDigit[ans]);
this.translate(num, length - 1, start + 1);
}
else if (length == 2)
{
if (num[start] == '0')
{
this.translate(num, length - 1, start + 1);
}
else
{
ans = (num[start]).charCodeAt(0) - ('0').charCodeAt(0);
ans *= 10 + num[start + 1] - ('0').charCodeAt(0);
if (ans >= 10 && ans <= 19)
{
process.stdout.write(" " + this.simpleDigit[ans]);
}
else
{
ans = (num[start]).charCodeAt(0) - ('0').charCodeAt(0);
process.stdout.write(" " + this.multiplex[ans]);
this.translate(num, length - 1, start + 1);
}
}
}
}
}
printWord(num, length)
{
if (length == 1)
{
// When number is single digit
this.translate(num, 1, 0);
return;
}
var counter = 0;
var digits = length;
var status = false;
while (counter < length && digits > 0)
{
if (num[counter] != '0')
{
// Tracker to find result
status = true;
if (digits == 1)
{
// When work with single digit
this.translate(num, 1, counter);
counter += 1;
digits -= 1;
}
else if (digits == 2)
{
// When work with two digit
this.translate(num, 2, counter);
counter += 2;
digits -= 2;
}
else
{
// When number digit length more than 2
// Case A
if (digits == 3 || digits % 2 == 0)
{
// When work with single digit
this.translate(num, 1, counter);
counter += 1;
digits -= 1;
process.stdout.write(" " + this.currency[digits]);
}
else
{
this.translate(num, 2, counter);
counter += 2;
digits -= 2;
process.stdout.write(" " + this.currency[digits]);
}
}
}
else
{
counter++;
digits--;
}
}
if (status == false)
{
// When In case get the all zero
process.stdout.write(" Zero ");
}
}
// Handles the request to print number words
words(num)
{
// Get the length
var length = num.length;
process.stdout.write(" Given Number : [" + num + "]\n");
if (length > 19)
{
process.stdout.write(" Number Out of range \n");
return;
}
// We assuming that given number is valid number
// And it is positive
this.printWord(num, length);
process.stdout.write("\n");
}
}

function main()
{
// Test Case
}
main();``````

#### Output

`````` Given Number : [123]
One Hundred Twenty Three
Given Number : [12345678]
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
Given Number : [8905463]
Eighty Nine Lakh Five Thousand Four Hundred Sity Three
Given Number : [100000000]
Zero Crore
Given Number : [1000100001]
One Arab One Lakh One
Given Number : [98815283]
Nine Crore Eighty Eight Lakh  Five Thousand Two Hundred Eighty Three
Given Number : [842321]
Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [5125235677842321]
Five Padma  Two Neel Fifty Two Kharab Thirty Five Arab Sity Seven Crore Seventy Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [9999]
Nine Thousand Nine Hundred Ninety Nine
Given Number : [86]
Eighty Six``````
``````#  Python 3 Program
#  Convert number to words

class NumberToWords :

def __init__(self) :
#  Allocate memory
self.currency = [""] * (20)
self.simpleDigit = [""] * (20)
self.multiplex = [""] * (10)
self.getDefault()

def getDefault(self) :
#  Define name of 20,30,40,50,60,70,80,90
self.multiplex[0] = ""
self.multiplex[1] = ""
self.multiplex[2] = "Twenty"
self.multiplex[3] = "Thirty"
self.multiplex[4] = "Forty"
self.multiplex[5] = "Fifty"
self.multiplex[6] = "Sity"
self.multiplex[7] = "Seventy"
self.multiplex[8] = "Eighty"
self.multiplex[9] = "Ninety"
#  Define name of 0...19
self.simpleDigit[0] = "Zero"
self.simpleDigit[1] = "One"
self.simpleDigit[2] = "Two"
self.simpleDigit[3] = "Three"
self.simpleDigit[4] = "Four"
self.simpleDigit[5] = "Five"
self.simpleDigit[6] = "Six"
self.simpleDigit[7] = "Seven"
self.simpleDigit[8] = "Eight"
self.simpleDigit[9] = "Nine"
self.simpleDigit[10] = "Ten"
self.simpleDigit[11] = "Eleven"
self.simpleDigit[12] = "Twelve"
self.simpleDigit[13] = "Thirteen"
self.simpleDigit[14] = "Fourteen"
self.simpleDigit[15] = "Fifteen"
self.simpleDigit[16] = "Sixteen"
self.simpleDigit[17] = "Seventeen"
self.simpleDigit[18] = "Eighteen"
self.simpleDigit[19] = "Nineteen"
#
#         You can change exist currency by country
#         Example
#         // International System
#         currency[0]  = ""
#         currency[1]  = ""
#         currency[2]  = "Hundred"
#         currency[3]  = "Thousand"
#         currency[4]  = "Ten Thousand"
#         currency[5]  = "Hundred Thousand"
#         currency[6]  = "Million"
#         currency[7]  = "Ten Million"
#         currency[8]  = "Hundred Million"
#         currency[9]  = "Billion"
#         currency[10] = "Ten Billion"
#         currency[11] = "Hundred Billion"
#         currency[12] = "Trillion"
#         currency[13] = "Ten Trillion "
#         currency[14] = "Hundred Trillion"
#         currency[18] = "Quintillion"
#         currency[19] = "Ten Quintillion"
#

#  Here mentioning indian currency
self.currency[0] = ""
self.currency[1] = ""
self.currency[2] = "Hundred"
self.currency[3] = "Thousand"
self.currency[4] = "Ten Thousand"
self.currency[5] = "Lakh"
self.currency[6] = "Ten Lakh"
self.currency[7] = "Crore"
self.currency[8] = "Ten Crore"
self.currency[9] = "Arab"
self.currency[10] = "Ten Arab"
self.currency[11] = "Kharab"
self.currency[12] = "Ten Kharab"
self.currency[13] = "Neel"
self.currency[14] = "Ten Neel"
self.currency[17] = "Shankh"
self.currency[18] = "Ten Shankh"
self.currency[19] = "Maha-Shankh"

def translate(self, num, length, start) :
if (length > 0) :
ans = 0
if (length == 1) :
ans = ord(num[start]) - ord('0')
print("", self.simpleDigit[ans], end = "")
self.translate(num, length - 1, start + 1)

elif(length == 2) :
if (num[start] == '0') :
self.translate(num, length - 1, start + 1)
else :
ans = ord(num[start]) - ord('0')
ans *= 10 + ord(num[start + 1]) - ord('0')
if (ans >= 10 and ans <= 19) :
print("", self.simpleDigit[ans], end = "")
else :
ans = ord(num[start]) - ord('0')
print("", self.multiplex[ans], end = "")
self.translate(num, length - 1, start + 1)

def printWord(self, num, length) :
if (length == 1) :
#  When number is single digit
self.translate(num, 1, 0)
return

counter = 0
digits = length
status = False
while (counter < length and digits > 0) :
if (num[counter] != '0') :
#  Tracker to find result
status = True
if (digits == 1) :
#  When work with single digit
self.translate(num, 1, counter)
counter += 1
digits -= 1

elif(digits == 2) :
#  When work with two digit
self.translate(num, 2, counter)
counter += 2
digits -= 2
else :
#  When number digit length more than 2
#  Case A
if (digits == 3 or digits % 2 == 0) :
#  When work with single digit
self.translate(num, 1, counter)
counter += 1
digits -= 1
print("", self.currency[digits], end = "")
else :
self.translate(num, 2, counter)
counter += 2
digits -= 2
print("", self.currency[digits], end = "")

else :
counter += 1
digits -= 1

if (status == False) :
#  When In case get the all zero
print(" Zero ", end = "")

#  Handles the request to print number words
def words(self, num) :
#  Get the length
length = len(num)
print(" Given Number : [{0}]".format(num))
if (length > 19) :
print(" Number Out of range ")
return

#  We assuming that given number is valid number
#  And it is positive
self.printWord(num, length)
print(end = "\n")

def main() :
#  Test Case

if __name__ == "__main__": main()``````

#### Output

`````` Given Number : [123]
One Hundred Twenty Three
Given Number : [12345678]
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
Given Number : [8905463]
Eighty Nine Lakh Five Thousand Four Hundred Sity Three
Given Number : [100000000]
Ten Crore
Given Number : [1000100001]
One Arab One Lakh One
Given Number : [98815283]
Nine Crore Eighty Eight Lakh Fifteen Thousand Two Hundred Eighty Three
Given Number : [842321]
Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [5125235677842321]
Five Padma Twelve Neel Fifty Two Kharab Thirty Five Arab Sity Seven Crore Seventy Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [9999]
Nine Thousand Nine Hundred Ninety Nine
Given Number : [86]
Eighty Six``````
``````#  Ruby Program
#  Convert number to words

class NumberToWords
# Define the accessor and reader of class NumberToWords
attr_accessor :currency, :simpleDigit, :multiplex

def initialize()
#  Allocate memory
self.currency = Array.new(20) {""}
self.simpleDigit = Array.new(20) {""}
self.multiplex = Array.new(10) {""}
self.getDefault()
end

def getDefault()
#  Define name of 20,30,40,50,60,70,80,90
multiplex[0] = ""
multiplex[1] = ""
multiplex[2] = "Twenty"
multiplex[3] = "Thirty"
multiplex[4] = "Forty"
multiplex[5] = "Fifty"
multiplex[6] = "Sity"
multiplex[7] = "Seventy"
multiplex[8] = "Eighty"
multiplex[9] = "Ninety"
#  Define name of 0...19
simpleDigit[0] = "Zero"
simpleDigit[1] = "One"
simpleDigit[2] = "Two"
simpleDigit[3] = "Three"
simpleDigit[4] = "Four"
simpleDigit[5] = "Five"
simpleDigit[6] = "Six"
simpleDigit[7] = "Seven"
simpleDigit[8] = "Eight"
simpleDigit[9] = "Nine"
simpleDigit[10] = "Ten"
simpleDigit[11] = "Eleven"
simpleDigit[12] = "Twelve"
simpleDigit[13] = "Thirteen"
simpleDigit[14] = "Fourteen"
simpleDigit[15] = "Fifteen"
simpleDigit[16] = "Sixteen"
simpleDigit[17] = "Seventeen"
simpleDigit[18] = "Eighteen"
simpleDigit[19] = "Nineteen"
#
#         You can change exist currency by country
#         Example
#         // International System
#         currency[0]  = ""
#         currency[1]  = ""
#         currency[2]  = "Hundred"
#         currency[3]  = "Thousand"
#         currency[4]  = "Ten Thousand"
#         currency[5]  = "Hundred Thousand"
#         currency[6]  = "Million"
#         currency[7]  = "Ten Million"
#         currency[8]  = "Hundred Million"
#         currency[9]  = "Billion"
#         currency[10] = "Ten Billion"
#         currency[11] = "Hundred Billion"
#         currency[12] = "Trillion"
#         currency[13] = "Ten Trillion "
#         currency[14] = "Hundred Trillion"
#         currency[18] = "Quintillion"
#         currency[19] = "Ten Quintillion"
#

#  Here mentioning indian currency
currency[0] = ""
currency[1] = ""
currency[2] = "Hundred"
currency[3] = "Thousand"
currency[4] = "Ten Thousand"
currency[5] = "Lakh"
currency[6] = "Ten Lakh"
currency[7] = "Crore"
currency[8] = "Ten Crore"
currency[9] = "Arab"
currency[10] = "Ten Arab"
currency[11] = "Kharab"
currency[12] = "Ten Kharab"
currency[13] = "Neel"
currency[14] = "Ten Neel"
currency[17] = "Shankh"
currency[18] = "Ten Shankh"
currency[19] = "Maha-Shankh"
end

def translate(num, length, start)
if (length > 0)
ans = 0
if (length == 1)
ans = (num[start]).ord - ('0').ord
print(" ", simpleDigit[ans])
self.translate(num, length - 1, start + 1)
elsif(length == 2)
if (num[start] == '0')
self.translate(num, length - 1, start + 1)
else
ans = (num[start]).ord - ('0').ord
ans *= 10 + (num[start + 1]).ord - ('0').ord
if (ans >= 10 && ans <= 19)
print(" ", simpleDigit[ans])
else
ans = (num[start]).ord - ('0').ord
print(" ", multiplex[ans])
self.translate(num, length - 1, start + 1)
end

end

end

end

end

def printWord(num, length)
if (length == 1)
#  When number is single digit
self.translate(num, 1, 0)
return
end

counter = 0
digits = length
status = false
while (counter < length && digits > 0)
if (num[counter] != '0')
#  Tracker to find result
status = true
if (digits == 1)
#  When work with single digit
self.translate(num, 1, counter)
counter += 1
digits -= 1
elsif(digits == 2)
#  When work with two digit
self.translate(num, 2, counter)
counter += 2
digits -= 2
else
#  When number digit length more than 2
#  Case A
if (digits == 3 || digits % 2 == 0)
#  When work with single digit
self.translate(num, 1, counter)
counter += 1
digits -= 1
print(" ", currency[digits])
else
self.translate(num, 2, counter)
counter += 2
digits -= 2
print(" ", currency[digits])
end

end

else
counter += 1
digits -= 1
end

end

if (status == false)
#  When In case get the all zero
print(" Zero ")
end

end

#  Handles the request to print number words
def words(num)
#  Get the length
length = num.length()
print(" Given Number : [", num ,"]\n")
if (length > 19)
print(" Number Out of range \n")
return
end

#  We assuming that given number is valid number
#  And it is positive
self.printWord(num, length)
print("\n")
end

end

def main()
#  Test Case
end

main()``````

#### Output

`````` Given Number : [123]
One Hundred Twenty Three
Given Number : [12345678]
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
Given Number : [8905463]
Eighty Nine Lakh Five Thousand Four Hundred Sity Three
Given Number : [100000000]
Ten Crore
Given Number : [1000100001]
One Arab One Lakh One
Given Number : [98815283]
Nine Crore Eighty Eight Lakh Fifteen Thousand Two Hundred Eighty Three
Given Number : [842321]
Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [5125235677842321]
Five Padma Twelve Neel Fifty Two Kharab Thirty Five Arab Sity Seven Crore Seventy Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [9999]
Nine Thousand Nine Hundred Ninety Nine
Given Number : [86]
Eighty Six
``````
``````/*
Scala Program
Convert number to words
*/
class NumberToWords(var currency: Array[String] , var simpleDigit: Array[String] , var multiplex: Array[String])
{
def this()
{
this(Array.fill[String](20)(""), Array.fill[String](20)(""), Array.fill[String](10)(""));
this.getDefault();
}
def getDefault(): Unit = {
// Define name of 20,30,40,50,60,70,80,90
multiplex(0) = "";
multiplex(1) = "";
multiplex(2) = "Twenty";
multiplex(3) = "Thirty";
multiplex(4) = "Forty";
multiplex(5) = "Fifty";
multiplex(6) = "Sity";
multiplex(7) = "Seventy";
multiplex(8) = "Eighty";
multiplex(9) = "Ninety";
// Define name of 0...19
simpleDigit(0) = "Zero";
simpleDigit(1) = "One";
simpleDigit(2) = "Two";
simpleDigit(3) = "Three";
simpleDigit(4) = "Four";
simpleDigit(5) = "Five";
simpleDigit(6) = "Six";
simpleDigit(7) = "Seven";
simpleDigit(8) = "Eight";
simpleDigit(9) = "Nine";
simpleDigit(10) = "Ten";
simpleDigit(11) = "Eleven";
simpleDigit(12) = "Twelve";
simpleDigit(13) = "Thirteen";
simpleDigit(14) = "Fourteen";
simpleDigit(15) = "Fifteen";
simpleDigit(16) = "Sixteen";
simpleDigit(17) = "Seventeen";
simpleDigit(18) = "Eighteen";
simpleDigit(19) = "Nineteen";
/*
You can change exist currency by country
Example
// International System
currency[0]  = "";
currency[1]  = "";
currency[2]  = "Hundred";
currency[3]  = "Thousand";
currency[4]  = "Ten Thousand";
currency[5]  = "Hundred Thousand";
currency[6]  = "Million";
currency[7]  = "Ten Million";
currency[8]  = "Hundred Million";
currency[9]  = "Billion";
currency[10] = "Ten Billion";
currency[11] = "Hundred Billion";
currency[12] = "Trillion";
currency[13] = "Ten Trillion ";
currency[14] = "Hundred Trillion";
currency[18] = "Quintillion";
currency[19] = "Ten Quintillion";
*/
// Here mentioning indian currency
currency(0) = "";
currency(1) = "";
currency(2) = "Hundred";
currency(3) = "Thousand";
currency(4) = "Ten Thousand";
currency(5) = "Lakh";
currency(6) = "Ten Lakh";
currency(7) = "Crore";
currency(8) = "Ten Crore";
currency(9) = "Arab";
currency(10) = "Ten Arab";
currency(11) = "Kharab";
currency(12) = "Ten Kharab";
currency(13) = "Neel";
currency(14) = "Ten Neel";
currency(17) = "Shankh";
currency(18) = "Ten Shankh";
currency(19) = "Maha-Shankh";
}
def translate(num: String, length: Int, start: Int): Unit = {
if (length > 0)
{
var ans: Int = 0;
if (length == 1)
{
ans = num(start) - '0';
print(" " + simpleDigit(ans));
this.translate(num, length - 1, start + 1);
}
else if (length == 2)
{
if (num(start) == '0')
{
this.translate(num, length - 1, start + 1);
}
else
{
ans = num(start) - '0';
ans *= 10 + num(start + 1) - '0';
if (ans >= 10 && ans <= 19)
{
print(" " + simpleDigit(ans));
}
else
{
ans = num(start) - '0';
print(" " + multiplex(ans));
this.translate(num, length - 1, start + 1);
}
}
}
}
}
def printWord(num: String, length: Int): Unit = {
if (length == 1)
{
// When number is single digit
this.translate(num, 1, 0);
return;
}
var counter: Int = 0;
var digits: Int = length;
var status: Boolean = false;
while (counter < length && digits > 0)
{
if (num(counter) != '0')
{
// Tracker to find result
status = true;
if (digits == 1)
{
// When work with single digit
this.translate(num, 1, counter);
counter += 1;
digits -= 1;
}
else if (digits == 2)
{
// When work with two digit
this.translate(num, 2, counter);
counter += 2;
digits -= 2;
}
else
{
// When number digit length more than 2
// Case A
if (digits == 3 || digits % 2 == 0)
{
// When work with single digit
this.translate(num, 1, counter);
counter += 1;
digits -= 1;
print(" " + currency(digits));
}
else
{
this.translate(num, 2, counter);
counter += 2;
digits -= 2;
print(" " + currency(digits));
}
}
}
else
{
counter += 1;
digits -= 1;
}
}
if (status == false)
{
// When In case get the all zero
print(" Zero ");
}
}
// Handles the request to print number words
def words(num: String): Unit = {
// Get the length
var length: Int = num.length();
print(" Given Number : [" + num + "]\n");
if (length > 19)
{
print(" Number Out of range \n");
return;
}
// We assuming that given number is valid number
// And it is positive
this.printWord(num, length);
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: NumberToWords = new NumberToWords();
// Test Case
}
}``````

#### Output

`````` Given Number : [123]
One Hundred Twenty Three
Given Number : [12345678]
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
Given Number : [8905463]
Eighty Nine Lakh Five Thousand Four Hundred Sity Three
Given Number : [100000000]
Ten Crore
Given Number : [1000100001]
One Arab One Lakh One
Given Number : [98815283]
Nine Crore Eighty Eight Lakh Fifteen Thousand Two Hundred Eighty Three
Given Number : [842321]
Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [5125235677842321]
Five Padma Twelve Neel Fifty Two Kharab Thirty Five Arab Sity Seven Crore Seventy Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [9999]
Nine Thousand Nine Hundred Ninety Nine
Given Number : [86]
Eighty Six``````
``````/*
Swift 4 Program
Convert number to words
*/
class NumberToWords
{
var currency: [String];
var simpleDigit: [String];
var multiplex: [String];
init()
{
// Allocate memory
self.currency = Array(repeating: "", count: 20);
self.simpleDigit = Array(repeating: "", count: 20);
self.multiplex = Array(repeating: "", count: 10);
self.getDefault();
}
func getDefault()
{
// Define name of 20,30,40,50,60,70,80,90
self.multiplex[0] = "";
self.multiplex[1] = "";
self.multiplex[2] = "Twenty";
self.multiplex[3] = "Thirty";
self.multiplex[4] = "Forty";
self.multiplex[5] = "Fifty";
self.multiplex[6] = "Sity";
self.multiplex[7] = "Seventy";
self.multiplex[8] = "Eighty";
self.multiplex[9] = "Ninety";
// Define name of 0...19
self.simpleDigit[0] = "Zero";
self.simpleDigit[1] = "One";
self.simpleDigit[2] = "Two";
self.simpleDigit[3] = "Three";
self.simpleDigit[4] = "Four";
self.simpleDigit[5] = "Five";
self.simpleDigit[6] = "Six";
self.simpleDigit[7] = "Seven";
self.simpleDigit[8] = "Eight";
self.simpleDigit[9] = "Nine";
self.simpleDigit[10] = "Ten";
self.simpleDigit[11] = "Eleven";
self.simpleDigit[12] = "Twelve";
self.simpleDigit[13] = "Thirteen";
self.simpleDigit[14] = "Fourteen";
self.simpleDigit[15] = "Fifteen";
self.simpleDigit[16] = "Sixteen";
self.simpleDigit[17] = "Seventeen";
self.simpleDigit[18] = "Eighteen";
self.simpleDigit[19] = "Nineteen";
/*
You can change exist currency by country
Example
// International System
currency[0] = "";
currency[1] = "";
currency[2] = "Hundred";
currency[3] = "Thousand";
currency[4] = "Ten Thousand";
currency[5] = "Hundred Thousand";
currency[6] = "Million";
currency[7] = "Ten Million";
currency[8] = "Hundred Million";
currency[9] = "Billion";
currency[10] = "Ten Billion";
currency[11] = "Hundred Billion";
currency[12] = "Trillion";
currency[13] = "Ten Trillion ";
currency[14] = "Hundred Trillion";
currency[18] = "Quintillion";
currency[19] = "Ten Quintillion";
*/
// Here mentioning indian currency
self.currency[0] = "";
self.currency[1] = "";
self.currency[2] = "Hundred";
self.currency[3] = "Thousand";
self.currency[4] = "Ten Thousand";
self.currency[5] = "Lakh";
self.currency[6] = "Ten Lakh";
self.currency[7] = "Crore";
self.currency[8] = "Ten Crore";
self.currency[9] = "Arab";
self.currency[10] = "Ten Arab";
self.currency[11] = "Kharab";
self.currency[12] = "Ten Kharab";
self.currency[13] = "Neel";
self.currency[14] = "Ten Neel";
self.currency[17] = "Shankh";
self.currency[18] = "Ten Shankh";
self.currency[19] = "Maha-Shankh";
}
func translate(_ num: [Character], _ length: Int, _ start: Int)
{
if (length > 0)
{
var ans: Int = 0;
if (length == 1)
{
ans = Int(UnicodeScalar(String(num[start]))!.value) - 48;
print(" ", self.simpleDigit[ans], terminator: "");
self.translate(num, length - 1, start + 1);
}
else if (length == 2)
{
if (num[start] == "0")
{
self.translate(num, length - 1, start + 1);
}
else
{
ans = Int(UnicodeScalar(String(num[start]))!.value) - 48;
ans *= 10 + Int(UnicodeScalar(String(num[start + 1]))!.value) - 48;
if (ans >= 10 && ans <= 19)
{
print("", self.simpleDigit[ans], terminator: "");
}
else
{
ans = Int(UnicodeScalar(String(num[start]))!.value) - 48;
print("", self.multiplex[ans], terminator: "");
self.translate(num, length - 1, start + 1);
}
}
}
}
}
func printWord(_ num: [Character], _ length: Int)
{
if (length == 1)
{
// When number is single digit
self.translate(num, 1, 0);
return;
}
var counter: Int = 0;
var digits: Int = length;
var status: Bool = false;
while (counter < length && digits > 0)
{
if (num[counter] != "0")
{
// Tracker to find result
status = true;
if (digits == 1)
{
// When work with single digit
self.translate(num, 1, counter);
counter += 1;
digits -= 1;
}
else if (digits == 2)
{
// When work with two digit
self.translate(num, 2, counter);
counter += 2;
digits -= 2;
}
else
{
// When number digit length more than 2
// Case A
if (digits == 3 || digits % 2 == 0)
{
// When work with single digit
self.translate(num, 1, counter);
counter += 1;
digits -= 1;
print("", self.currency[digits], terminator: "");
}
else
{
self.translate(num, 2, counter);
counter += 2;
digits -= 2;
print("", self.currency[digits], terminator: "");
}
}
}
else
{
counter += 1;
digits -= 1;
}
}
if (status == false)
{
// When In case get the all zero
print(" Zero ", terminator: "");
}
}
// Handles the request to print number words
func words(_ num: String)
{
// Get the length
let length: Int = num.count;

print(" Given Number : [", num ,"]");

if (length > 19)
{
print(" Number Out of range ");
return;
}

// We assuming that given number is valid number
// And it is positive
self.printWord(Array(num), length);
print(terminator: "\n");
}
}
func main()
{
// Test Case
}
main();``````

#### Output

`````` Given Number : [ 123 ]
One Hundred Twenty  Three
Given Number : [ 12345678 ]
One Crore Twenty  Three Lakh Forty  Five Thousand  Six Hundred Seventy  Eight
Given Number : [ 8905463 ]
Eighty  Nine Lakh  Five Thousand  Four Hundred Sity  Three
Given Number : [ 100000000 ]
Ten Crore
Given Number : [ 1000100001 ]
One Arab  One Lakh  One
Given Number : [ 98815283 ]
Nine Crore Eighty  Eight Lakh Fifteen Thousand  Two Hundred Eighty  Three
Given Number : [ 842321 ]
Eight Lakh Forty  Two Thousand  Three Hundred Twenty  One
Given Number : [ 5125235677842321 ]
Five Padma Twelve Neel Fifty  Two Kharab Thirty  Five Arab Sity  Seven Crore Seventy  Eight Lakh Forty  Two Thousand  Three Hundred Twenty  One
Given Number : [ 9999 ]
Nine Thousand  Nine Hundred Ninety  Nine
Given Number : [ 86 ]
Eighty  Six``````
``````/*
Kotlin Program
Convert number to words
*/
class NumberToWords
{
var currency: Array<String>;
var simpleDigit: Array<String>;
var multiplex: Array<String>;
constructor()
{
// Allocate memory
this.currency = Array(20){""};
this.simpleDigit = Array(20){""};
this.multiplex = Array(10){""};
this.getDefault();
}
fun getDefault(): Unit
{
// Define name of 20,30,40,50,60,70,80,90
multiplex[0] = "";
multiplex[1] = "";
multiplex[2] = "Twenty";
multiplex[3] = "Thirty";
multiplex[4] = "Forty";
multiplex[5] = "Fifty";
multiplex[6] = "Sity";
multiplex[7] = "Seventy";
multiplex[8] = "Eighty";
multiplex[9] = "Ninety";
// Define name of 0...19
simpleDigit[0] = "Zero";
simpleDigit[1] = "One";
simpleDigit[2] = "Two";
simpleDigit[3] = "Three";
simpleDigit[4] = "Four";
simpleDigit[5] = "Five";
simpleDigit[6] = "Six";
simpleDigit[7] = "Seven";
simpleDigit[8] = "Eight";
simpleDigit[9] = "Nine";
simpleDigit[10] = "Ten";
simpleDigit[11] = "Eleven";
simpleDigit[12] = "Twelve";
simpleDigit[13] = "Thirteen";
simpleDigit[14] = "Fourteen";
simpleDigit[15] = "Fifteen";
simpleDigit[16] = "Sixteen";
simpleDigit[17] = "Seventeen";
simpleDigit[18] = "Eighteen";
simpleDigit[19] = "Nineteen";
/*
You can change exist currency by country
Example
// International System
currency[0]  = "";
currency[1]  = "";
currency[2]  = "Hundred";
currency[3]  = "Thousand";
currency[4]  = "Ten Thousand";
currency[5]  = "Hundred Thousand";
currency[6]  = "Million";
currency[7]  = "Ten Million";
currency[8]  = "Hundred Million";
currency[9]  = "Billion";
currency[10] = "Ten Billion";
currency[11] = "Hundred Billion";
currency[12] = "Trillion";
currency[13] = "Ten Trillion ";
currency[14] = "Hundred Trillion";
currency[18] = "Quintillion";
currency[19] = "Ten Quintillion";
*/
// Here mentioning indian currency
currency[0] = "";
currency[1] = "";
currency[2] = "Hundred";
currency[3] = "Thousand";
currency[4] = "Ten Thousand";
currency[5] = "Lakh";
currency[6] = "Ten Lakh";
currency[7] = "Crore";
currency[8] = "Ten Crore";
currency[9] = "Arab";
currency[10] = "Ten Arab";
currency[11] = "Kharab";
currency[12] = "Ten Kharab";
currency[13] = "Neel";
currency[14] = "Ten Neel";
currency[17] = "Shankh";
currency[18] = "Ten Shankh";
currency[19] = "Maha-Shankh";
}
fun translate(num: String, length: Int, start: Int): Unit
{
if (length>0)
{
var ans: Int ;
if (length == 1)
{
ans = num[start] - '0';
print(" " + simpleDigit[ans]);
this.translate(num, length - 1, start + 1);
}
else
if (length == 2)
{
if (num[start] == '0')
{
this.translate(num, length - 1, start + 1);
}
else
{
ans = num[start] - '0';
ans *= 10 + (num[start + 1] - '0');
if (ans>= 10 && ans <= 19)
{
print(" " + simpleDigit[ans]);
}
else
{
ans = num[start] - '0';
print(" " + multiplex[ans]);
this.translate(num, length - 1, start + 1);
}
}
}
}
}
fun printWord(num: String, length: Int): Unit
{
if (length == 1)
{
// When number is single digit
this.translate(num, 1, 0);
return;
}
var counter: Int = 0;
var digits: Int = length;
var status: Boolean = false;
while (counter<length && digits>0)
{
if (num[counter] != '0')
{
// Tracker to find result
status = true;
if (digits == 1)
{
// When work with single digit
this.translate(num, 1, counter);
counter += 1;
digits -= 1;
}
else
if (digits == 2)
{
// When work with two digit
this.translate(num, 2, counter);
counter += 2;
digits -= 2;
}
else
{
// When number digit length more than 2
// Case A
if (digits == 3 || digits % 2 == 0)
{
// When work with single digit
this.translate(num, 1, counter);
counter += 1;
digits -= 1;
print(" " + currency[digits]);
}
else
{
this.translate(num, 2, counter);
counter += 2;
digits -= 2;
print(" " + currency[digits]);
}
}
}
else
{
counter += 1;
digits -= 1;
}
}
if (status == false)
{
// When In case get the all zero
print(" Zero ");
}
}
// Handles the request to print number words
fun words(num: String): Unit
{
// Get the length
var length: Int = num.count();
print(" Given Number : [" + num + "]\n");
if (length > 19)
{
print(" Number Out of range \n");
return;
}
// We assuming that given number is valid number
// And it is positive
this.printWord(num, length);
print("\n");
}
}
fun main(args: Array<String>): Unit
{
// Test Case
}``````

#### Output

`````` Given Number : [123]
One Hundred Twenty Three
Given Number : [12345678]
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
Given Number : [8905463]
Eighty Nine Lakh Five Thousand Four Hundred Sity Three
Given Number : [100000000]
Ten Crore
Given Number : [1000100001]
One Arab One Lakh One
Given Number : [98815283]
Nine Crore Eighty Eight Lakh Fifteen Thousand Two Hundred Eighty Three
Given Number : [842321]
Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [5125235677842321]
Five Padma Twelve Neel Fifty Two Kharab Thirty Five Arab Sity Seven Crore Seventy Eight Lakh Forty Two Thousand Three Hundred Twenty One
Given Number : [9999]
Nine Thousand Nine Hundred Ninety Nine
Given Number : [86]
Eighty Six``````

## Explanation

The algorithm starts by defining arrays to store the word representation of digits, currency names, and multiples of ten. The `translate` function recursively converts a given part of the number into words. It handles single-digit numbers and two-digit numbers separately. The `printWord` function iterates through the digits of the number and determines the appropriate translation based on the number of digits remaining. It prints the corresponding word and currency name. Finally, the `numToWords` function takes an input number, checks its length, and calls `printWord` to print the word representation of the number.

Let's take an example to understand the execution of the code:

```    ```
numToWords("12345678")
```
```

The given number is "12345678". The `printWord` function will iterate through each digit and translate it into words:

1. Digit '1': Translated to "One" and printed.
2. Digit '2': Translated to "Twenty" and printed.
3. Digit '3': Translated to "Thirty" and printed.
4. Digit '4': Translated to "Forty" and printed.
5. Digit '5': Translated to "Fifty" and printed.
6. Digit '6': Translated to "Sixty" and printed.
7. Digit '7': Translated to "Seventy" and printed.
8. Digit '8': Translated to "Eight" and printed.

The `printWord` function also determines the appropriate currency names based on the number of digits remaining and prints "Crore" and "Lakh" accordingly. The final output will be:

```    ```
One Crore Twenty Three Lakh Forty Five Thousand Six Hundred Seventy Eight
```
```

## Time Complexity

The time complexity of the algorithm depends on the number of digits in the input number. Let's denote the number of digits as 'n'. Since the algorithm iterates through each digit only once, the time complexity is O(n). This is because the size of the input number directly affects the number of iterations performed by the algorithm.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post