# Problem Solving: Number-to-word Conversion

Learn to write a program using functions that takes a number as input and displays all its digits in words.

In this lesson, we’ll write a program that prints an n-length number into words using functions.

So, let’s start with the simpler version of the problem.

## 3-digit number to word conversion

Write and run a program that takes a three-digit number as input and prints all digits in words.

**Sample input**

```
564
```

**Sample output**

```
Five Six Four
```

So we have three main steps in our program:

- We take the number as input.
- We separate each digit.
- We display each digit in words on the console (but remember not in the reverse order).

To write a program for the above problem using functions, see how we break down the problem.

Our `main()`

function will be very simple:

int main(){int num;cin>>num;numberToWord(num);return 0;}

Let’s say we have a num variable as an integer, and we pass that num to thenumber3DToWord() function as an argument. This function will first check whether the number is a 3-digit number or not.

- If the number is not a 3-digit number, it should just display a “Wrong input” message and return it.
- If the number is a 3-digit, it will be split, and every digit is stored separately into
`d1`

,`d2`

, and`d3`

. - Now, every digit is printed into words separately.

void number3DToWord((int num){if(num<000 && num>999) // To check number is three-digit{cout<<"Wrong Input"<<endl;return;}int d1,d2, d3;//To split three-digit number:d3=num%10;num=num/10;d2=num%10;d1=num/10;// Printing the corresponding words of d3, d2, and d1 should be done here...}

To print numbers in words, we’ll need another function `printWord()`

.

- The return type of
`printWord()`

would then be`void`

as it will be printing the word on the console corresponding to a digit. - We also have a
`switch`

statement in which we will compare the`digit`

and print the corresponding string on the console. For example, if a`digit`

is`2`

, the program will print`Two`

on the console.

So, our `printWord()`

function would be:

void printWord(int digit){switch(digit){case 1:cout<<"One"<<"\t";break;case 2:cout<<"Two"<<"\t";break;case 3:cout<<"Three"<<"\t";break;case 4 :cout<<"Four"<<"\t";break;case 5:cout<<"Five"<<"\t";break;case 6:cout<<"Six"<<"\t";break;case 7:cout<<"Seven"<<"\t";break;case 8:cout<<"Eight"<<"\t";break;case 9:cout<<"Nine"<<"\t";break;case 0:cout<<"Zero"<<"\t";break;}}

The `printWord()`

function takes a `digit`

as parameter. We have a `switch`

statement in which we have an expression to evaluate the `digit`

, and the corresponding `case`

will print the word on the console.

We will call this `printWord()`

function in `number3DToWord()`

.

void number3DToWord(int num){...printWord(d1);printWord(d2);printWord(d3);}

Here, we have an interactive animation for better understanding:

Next, let’s write the complete code below and run it to see the output:

#include <iostream> using namespace std; //This Function Will Print The Word corresponding to the digit passed. void printWord(int); //This Function is Separating the digits and store them in three different variables. void number3DToWord(int); int main() { int num; cout << "The number: "; cin>>num; number3DToWord(num); // passing input to function return 0; } void printWord(int digit) { switch(digit) { case 1: cout<<"One"<<"\t"; break; case 2: cout<<"Two"<<"\t"; break; case 3: cout<<"Three"<<"\t"; break; case 4 : cout<<"Four"<<"\t"; break; case 5: cout<<"Five"<<"\t"; break; case 6: cout<<"Six"<<"\t"; break; case 7: cout<<"Seven"<<"\t"; break; case 8: cout<<"Eight"<<"\t"; break; case 9: cout<<"Nine"<<"\t"; break; case 0: cout<<"Zero"<<"\t"; break; } } void number3DToWord(int num) { if(num<000 && num>999) // Checking input number is 3-digit or not { cout<<"wrong Input"<<endl; return; } int d1,d2,d3; // 3-digit number split into 3 digits: d3=num%10; num=num/10; d2=num%10; d1=num/10; printWord(d1); // calling function to print a digit into word printWord(d2);// calling function to print a digit into word printWord(d3);// calling function to print a digit into word }

- In
**line 11**, we pass input`num`

to function as a parameter. - In
**line 51**, we take input`num`

in function as a parameter. - In
**lines 60–63**, we split`num`

into digits and stored them into`d1`

,`d2`

and`d3`

. - In
**lines 65–67**, we call the`printWord`

function and pass digit to the`printWord`

for printing words. - In
**lines 14–49**, we take`digit`

as a parameter and print the word according to that`digit`

in the`switch`

statement.

**Instruction:** Execute the above code step by step, put a breakpoint on **line 11**, and use step into to see how the function gets executed. Look closely at the watches and notice how the variables as parameters are received and the local variables inside the function get the value and fulfill the appropriate task.

## Number-to-word conversion for two input numbers

Now, we want to take two inputs and perform the same task. So, what changes will we make to perform this task?

**Sample input**

```
121
224
```

**Sample output**

```
One Two One
Two Two Four
```

To perform this task, we’ll call the `numberToWord()`

two times on the given inputs. We will not write the same function code for multiple inputs. This is the beauty of functions. We can just call a function anywhere we need its functionality.

Let’s see what changes we need to make in the program:

#include <iostream> using namespace std; //This Function Will Print The Word corresponding to the digit passed. void printWord(int); //This Function is Separating the digits and store them in three different variables. void numberToWord(int); int main() { int num1,num2; cout << "Enter the two numbers: " << endl; cin >> num1 >> num2; numberToWord(num1); // passing input to function numberToWord(num2); // passing input to function return 0; } void printWord(int digit) { switch(digit) { case 1: cout<<"One"<<"\t"; break; case 2: cout<<"Two"<<"\t"; break; case 3: cout<<"Three"<<"\t"; break; case 4 : cout<<"Four"<<"\t"; break; case 5: cout<<"Five"<<"\t"; break; case 6: cout<<"Six"<<"\t"; break; case 7: cout<<"Seven"<<"\t"; break; case 8: cout<<"Eight"<<"\t"; break; case 9: cout<<"Nine"<<"\t"; break; case 0: cout<<"Zero"<<"\t"; break; } } void numberToWord(int num) { if(num<000 && num>999) // Checking input number is 3-digit or not { cout<<"wrong Input"<<endl; } int d1,d2,d3; // 3-digit number split into 3 digits: d3=num%10; num=num/10; d2=num%10; num=num/10; d1=num; printWord(d1); // calling function to print a digit into word printWord(d2);// calling function to print a digit into word printWord(d3);// calling function to print a digit into word }

- In
**line 10**, we take input,`num1`

and`num2`

. - In
**lines 11–12**, we call a function,`numberToWord`

, twice for these two inputs.

So far, we’ve written the program to convert a number to a word for two numbers with 3-digit restrictions. Now we’ll extend the challenge with no restriction on the number of digits.

## Number-to-word conversion for variable length

Our first attempt will have a small problem. Lets see if you can comprehend it.

### A failed attempt

Let’s first try to program the wrong version. There is a quiz coming up, so look at the code carefully.

void numberToWord(int num);int main(){int num;cin>>num;numberToWord(num);return 0;}void numberToWord(int n){int adigit;while(n!=0){adigit = n%10;printWord(adigit);n=n/10;}}

Solve the quiz below.

Number-to-word conversion

What will be the output of the program on the input of `12345`

?

`Five Four Three Two One`

`One Two Three Four Five`

As we can see in the quiz above, the output is in the reverse order. The reason is that the number is shrinking from the rightmost position—from the least significant digit to the left position, which is the most significant digit.

Let’s try to solve the problem in such a way that the number is shrunk from the left side. How can we solve this problem?

### A correct attempt

To solve this problem, we first need to find the number of digits in `num`

. We will create a function, `numOfDigit()`

, to pass `num`

as a parameter. Look at the following code and read its comments carefully:

int numOfDigit(int num){int c = 0;while(num!=0){c++; // c++ counts digits andnum=num/10; // num = num/10 is shrinking by 1 digit}return c; //return # of digits in num}

The next step is to split digits from left to right, i.e., most significant to least significant digit, and print them in words side by side.

#### Hint

Let us say we have `num = 1234`

and `divisor = 1000`

:

- If we divide
`num / divisor`

, it will give us`1`

as the most significant digit. - If we take
`num % divisor`

, it gives us`234`

, i.e., shrinking of the most significant digit. - If we change the
`divisor`

to`100`

and repeat the first two steps, it will give us`2`

and`34`

, respectively.

We need a `power()`

function that will take a power of `10`

with a count of the first digit. For example, we have `1234`

and the `numOfDigit()`

function, it will return the count as `4`

. Now, if we take a power of `10`

with `4-1`

, it will return `1000`

.

int power(int v, int t){int result = 1;for(int i=1; i<=t; i++)result*=v;return result;}

The next step is printing numbers into words. We will create another function, `printNum2Words`

, to take `num`

as a parameter.

- The first step is to call the
`numOfDigit`

function, which will return a count of digits. - The second step is to take a power of
`10`

with a count of digits using the`power()`

function. This function will return a number. - The third step is to write a
`for`

loop in which we will divide the number with the result returned from the`power()`

function and print the digit into a word using the`printWord()`

function.

void printNum2Words(int num){ // For example: num=1234int nd = numOfDigit(num); // numOfDigit will return count which is 4int d = power(10, nd-1); // power function will return 1000for(int i=1; i<=nd; i++){int digit = num/d; // 1234/1000, which is equal to 1printWord(digit); // print One on consolenum = num%d; // 1234%1000 which is equal to 234d/=10; // d=100/* In the next iteration, we will divide 234 by 100 and print Two; after that,we will divide 34 by 10 and print Three and so on. */}}

Let’s write a complete code below:

#include <iostream> using namespace std; int numOfDigit(int num) { int c = 0; while(num!=0) c++, num=num/10; return c; } void printWord(int digit) { switch(digit) { case 1: cout<<"One"<<"\t"; break; case 2: cout<<"Two"<<"\t"; break; case 3: cout<<"Three"<<"\t"; break; case 4 : cout<<"Four"<<"\t"; break; case 5: cout<<"Five"<<"\t"; break; case 6: cout<<"Six"<<"\t"; break; case 7: cout<<"Seven"<<"\t"; break; case 8: cout<<"Eight"<<"\t"; break; case 9: cout<<"Nine"<<"\t"; break; case 0: cout<<"Zero"<<"\t"; break; } } int power(int v, int t) { int result = 1; for(int i=1; i<=t; i++) result*=v; return result; } void printNum2Words(int num) { int nd = numOfDigit(num); int d = power(10, nd-1); for(int i=1; i<=nd; i++) { int digit = num/d; printWord(digit); num = num%d; d/=10; } } int main() { int h; // how many numbers a user wants to test on; int num; cout << "How many tests: "; cin>>h; for(int i=1; i<=h; i++) { cout <<"\nTest #"<< i<<". "; cin>>num; // 12345 printNum2Words(num); } return 0; }

We have solved one problem with multiple solutions in this lesson. First, we write a function for printing a 3-digit number into words, then we will move towards two 3-digit numbers, and we see how we can do the same task by calling the same function for multiple inputs. Finally, we modify the code and solve the above task with n-digit numbers.

#### Exercise: Find logical error

Look at the following implementation:

void printNum2Words(int num){int nd = numOfDigit(num);int d = power(10, nd-1);for(int i=1;num!=0 ; // we changed the conditioni++){int digit = num/d;printWord(digit);num = num%d;d/=10;}}

Replace the `printNum2Words()`

function in the above playground code with this code of `printNum2Words()`

. This code will run fine for inputs `1234`

and `3204`

. But this code will give a wrong output on the input `12000`

or `53400`

. How can we fix that?

We’ve written the

`for`

loop in three lines (lines 6–8). This is acceptable and correct and can be written like this.