# How to convert a given number into words in C++

In this tutorial, we are going to learn to **C****onvert a given number into words in C++. **Example (**123 to One Hundred Twenty Three**).

**Introduction**

To perform this program we are going to broke the entered number into pieces. The number is broken into **Ones** and **Tens.**

## C++ Program to convert number into words

**Code:**

#include<iostream> using namespace std; void convert(int value) { char *first[20] = {"ZERO", "ONE", "TWO", "THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN", "ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTTEEN","NINETEEN"}; char *second[10] = {"", "TEN", "TWENTY", "THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"}; if(value<0) { cout<<" "; convert(-value); } else if(value>=1000) { convert(value/1000); cout<<" THOUSAND"; if(value % 1000) { if(value % 1000 < 100) { cout << " AND"; } cout << " " ; convert(value % 1000); } } else if(value >= 100) { convert(value / 100); cout<<" HUNDRED"; if(value % 100) { cout << " AND "; convert (value % 100); } } else if(value >= 20) { cout << second[value / 10]; if(value % 10) { cout << " "; convert(value % 10); } } else { cout<<first[value]; } return; } int main() { int number; cout<<"Enter a number : "; cin>>number; convert(number); return 0; }

**Output:**

Enter a number : 786 SEVEN HUNDRED AND EIGHTY SIX

**Explanation:**

Firstly, we create two arrays of string. The first one named “**first**” contains spelling of numbers from zero to ninety excluding “**second**” values like ten, twenty, etc.

Secondly, we need to separate the numbers from the value entered. We do this using “%”(Modulus Operator) and “/”(Division Operator).

**Example:**

If we want to extract digits from number “786”.

The value of (786%10) is 6

Then we divide 786 by 10 it gives 78.

Again to get the last digit 78%10 is 8.

To get the last digit again divide it by 10 78/10 it gives 7.

To get the final digit we perform the same step 7%10 gives 7.

So, the number 786 is now divided into a single digit.

In the code, we first classify the number entered into its range between (value < 0), (value >= 1000 ), (value >= 100), (value >= 20) and else the ones.

Every condition have is used to print the number and after that there postfix like hundred, thousand etc. After the print of the first digit, then condition calls the program itself. This continues till the last digit of the number is printed.

The function checks the single digit with the matching array and prints its correct conversion to words.

#### Conclusion

This is how we can convert any number into words. The same method can be put in use in many places any number can be given a special value and can be used to create a crypto program.

Also Checkout:

Reverse words in a string in C++ without using any built-in function in CPP

Thank you so much!

It came in handy to me.