











C++ program for Converting digit/number to words
Converting digit/number to words
The conversion of numbers in words is just a conversion of numeric values to the English format of reading numbers. This code supports the conversion of numbers from 0 – 9999 in English format. Digits have different places when read from its ones place to above. Different places for numbers are:-
- Single digits:- Ones
- Two Digits:-Tens
- Three Digits:- Hundreds
- Four Digits:- Thousands


Algorithm
- Taking input as a string from the user.
- Check the length of the input.
- if the length is zero print ’empty’ and if the length is greater than 4 print ‘give a string of specific length’
- if length id between 1 – 4, Create arrays for different values.
- Checking the length of the string.
- According to the place of the digit, we will show the output.
C++ Code:-
//C++ program
//convert number to text
#include<iostream>
#include<string.h>
using namespace std;
//main Program
void numToWords(string num)
{
int length_of_string = strlen(num);
if (length_of_string == 0){
cout<<“String is Empty”;
return;
}
if (length_of_string > 4){
cout<<“Please enter the string with supported length”;
return;
}
string ones_digits = {“zero”, “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”};
string tens_digits = {“”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”, “fifteen”, “sixteen”, “seventeen”, “eighteen”,“nineteen”};
string multiple_of_ten = {“”, “”, “twenty”, “thirty”, “forty”, “fifty”, “sixty”, “seventy”, “eighty”, “ninety”};
string power_of_ten = {“hundred”, “thousand”};
cout<<num<<“:\n“;
if (length_of_string == 1){
cout<<ones_digits[num[0] – ‘0’];
return;
}
int x=0;
while (x < strlen(num)){
if(length_of_string >= 3){
if (num[x] – 48 != 0){
cout<<ones_digits[num[x] – 48]<<“\n“;
cout<<power_of_ten[length_of_string – 3]<<“\n“;
length_of_string–;
}
}
else{
if (num[x] – 48 == 1){
sum = (num[x] – 48 + num[x] – 48);
cout<<tens_digits[sum]);
return;
}
else if(num[x] – 48 == 2 and num[x + 1] – 48 == 0){
cout<<“twenty”;
return;
}
else{
int i = num[x] – 48;
if(i > 0){
print(multiple_of_ten[i], end = ” “);
}
else{
print(“”, end = “”);
}
x += 1;
if(num[x] – 48 != 0){
cout<<ones_digits[num[x] – 48];
}
}
}
x++;
}
}
int main()
{
numToWords(“1121”);
return 0;
}
Output:
1121 : one thousand one hundred twenty one


- Highest Common Factor(HCF): C | C++ | Java | Python
- Lowest Common Multiple (LCM) : C | C++ | Java | Python
- Greatest Common Divisor : C | C++ | Java | Python
- Binary to Decimal to conversion : C | C++ | Java | Python
- Binary to Octal conversion : C | C++ | Java | Python
- Decimal to Binary conversion: C | C++ | Java | Python
- Decimal to octal Conversion: C | C++ | Java | Python
- Octal to Binary conversion : C | C++ | Java | Python
- Octal to Decimal conversion : C | C++ | Java | Python
- Quadrants in which a given coordinate lies : C | C++ | Java | Python
- Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
- Maximum number of handshakes: C | C++ | Java | Python
- Addition of two fractions: C | C++ | Java | Python
- Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
- Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
- Count possible decoding of a given digit sequence : C | C++ | Java
- Check whether a character is a vowel or consonant : C | C++ | Java | Python
- Check whether a character is a alphabet or not : C | C++ | Java | Python
- Calculate the area of a circle : C | C++ | Java | Python
- Find the ASCII value of a character : C | C++ | Java | Python
- Find the prime numbers between 1 to 100 : C | C++ | Java | Python
- Calculate the number of digits in an integer : C | C++ | Java | Python
- Convert digit/number to words : C | C++ | Java | Python
- Counting number of days in a given month of a year: C | C++ | Java | Python
- Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
- Finding number of integers which has exactly x divisors: C | C++ | Java | Python
- Finding Roots of a quadratic equation : C | C++ | Java | Python
Login/Signup to comment