# Integer to English words

Difficulty Level Hard
StringViews 2944

In problem “Integer to English words” we have given a non-negative integer and the tasks to convert that integer into its numerical words or we get an input of a number, any number, and our task is to represent that number in a string form. Let’s see one example, the number “765” should be represented as “seven hundred and sixty five”. So we get to deal with every specific number in a number.

## Example

Input:

123

Output:

One hundred twenty three

Input:

4517

Output:

four thousand five hundred seventeen

You have to write a program to convert any integer into its numerical word form

## Algorithm

1. Declare a Long Integer variable and get the input.
2. Declare an array for one place and initialize all the ones place words in it, and another array for tens place and initialize with tens of places words as written in code.
3. Pass the dividend of that number with a suitable string to the function which can get the ones and tens words and add it to a temporary string.
4. Return that temporary string.
5. The function can work on ones and tens places can get suitable words for it from an array and store it in string result.
6. And it will keep appending the string until it comes to one’s place for number.
7. And we get the output of a number which is representing as a string.

## Explanation

Our main idea is to start from a maximum value. So declared two arrays in which one store all the words which are less than 20. And one which stores all the values in which all the multiples of ten are stored which are less than 100, as written in code.

So we will go through this with an example.

Input is given as 56543

We take input and pass it to our function getWords where we declare the string result and we are going to pass dividend. So first it is going to pass which divides the number by 10000000 and we get dividend as 0. 0 passes with string crore. It will come in the Words function where it checks the 0th place of tens and ones where it finds nothing and now returns with an empty temp string.

So now, it is going to pass the number which divides the number by 100000 and modular with 100 and we get dividend as 0. 0 passes with string lakh now. It will come in the Words function where it checks the 0th place of tens and ones it the find nothing and now return with an empty temp string.

So now, it is going to pass number which divides the number by 1000 and modular with 100 and we get dividend as 0, 56 passes with string thousand now. It will come in the Words function where it takes the dividend as 5 and remainder as 6 with string pass as thousand, these all are stored at temporary string which is “fifty six thousand” and it returns in the result.

Now, it is going to pass the number which divides the number by 100 and modular with 10 and we get dividend as 5. 5 passes with string hundred now. It will come in the Words function where it takes the dividend as 5. And remainders as 0 with the string passed as a hundred so after searching in the array and adding it in the temporary string will become “fifty six thousand five hundred”.

Now after checking if block, it will print “and” and now again. It is going to pass number which modular with 100 and passes 43. And we get dividend as 4 and remainder as 3 that is stored in the temporary string. After finding out the words for a given number and temp return into a result.

And now the result will become “fifty six thousand five hundred and forty three”.

## Implementation

### C++ program for Integer to English words

#include <iostream>
using namespace std;

string ones_place[] = { "", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ",
"nine ", "ten ", "eleven ", "twelve ",
"thirteen ", "fourteen ", "fifteen ",
"sixteen ", "seventeen ", "eighteen ",
"nineteen "
};

string tens_place[] = { "", "", "twenty ", "thirty ", "forty ",
"fifty ", "sixty ", "seventy ", "eighty ",
"ninety "
};

string Words(int num, string s)
{
string temp = "";
if (num > 19)
{
temp += tens_place[num / 10] + ones_place[num % 10];
}
else
{
temp += ones_place[num];
}
if (num)
{
temp += s;
}
return temp;
}

string getWords(long number)
{
string result;
result += Words((number / 10000000), "crore ");

result += Words(((number / 100000) % 100), "lakh ");

result += Words(((number / 1000) % 100), "thousand ");

result += Words(((number / 100) % 10), "hundred ");

if (number> 100 && number % 100)
{
result += "and ";
}
result += Words((number % 100), "");

return result;
}
int main()
{
cout << getWords(4517) << endl;
return 0;
}
four thousand five hundred and seventeen

### Java program for Integer to English words

import java.text.DecimalFormat;

public class Integer_to_words {

private static final String[] tens_names = {
"", " ten", " twenty",
" thirty", " forty", " fifty", " sixty", " seventy", " eighty",
" ninety"
};

private static final String[] num_Names = {
"", " one", " two", " three",
" four", " five", " six", " seven", " eight", " nine", " ten",
" eleven", " twelve", " thirteen", " fourteen", " fifteen",
" sixteen", " seventeen", " eighteen", " nineteen"
};

private static String Upto_one_thousand(int number) {
String soFar;

if (number % 100<20) {
soFar = num_Names[number % 100];
number /= 100;
} else {
soFar = num_Names[number % 10];
number /= 10;

soFar = tens_names[number % 10] + soFar;
number /= 10;
}
if (number == 0)
return soFar;
return num_Names[number] + " hundred" + soFar;
}

public static String convert(long number) {
// 0 to 999 999 999 999
if (number == 0) {
return "zero";
}

String snumber = Long.toString(number);

snumber = df.format(number);

// XXXnnnnnnnnn
int billions = Integer.parseInt(snumber.substring(0, 3));
// nnnXXXnnnnnn
int millions = Integer.parseInt(snumber.substring(3, 6));
// nnnnnnXXXnnn
int hundredThousands = Integer.parseInt(snumber.substring(6, 9));
// nnnnnnnnnXXX
int thousands = Integer.parseInt(snumber.substring(9, 12));

switch (billions) {
case 0:
break;
case 1:
tradBillions = Upto_one_thousand(billions) + " billion ";
break;
default:
tradBillions = Upto_one_thousand(billions) + " billion ";
}

switch (millions) {
case 0:
break;
case 1:
tradMillions = Upto_one_thousand(millions) + " million ";
break;
default:
tradMillions = Upto_one_thousand(millions) + " million ";
}

switch (hundredThousands) {
case 0:
break;
case 1:
break;
default:
" thousand ";
}

// remove extra spaces!
return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
}

public static void main(String[] args) {
System.out.println(convert(4517));
}
}
four thousand five hundred seventeen

## Complexity Analysis for Integer to English words

### Time Complexity

The loop runs for a constant amount of time so the complexity will be O(1).

### Auxiliary Space

The auxiliary space is O(1) as no extra space is required.

Translate »