Number to words | Program to convert a given number to words

Program to convert a given number to words is discussed here. For example, if “1234” is given as input, the output should be “one thousand two hundred and thirty four”.

Sample input: 2
Sample output: two

Sample input: 9923
Sample output: nine thousand nine hundred twenty three

Program to convert a given number to words

//This program can be used to convert a number to words

#include <stdio.h>
#include <string.h>
void convert(char *num)
{
int len = strlen(num); // find no of digit
/* no number */
if (len == 0) {
fprintf(stderr, “empty string\n”);
return;
}

char *single_digits[] = { “zero”, “one”, “two”, “three”, “four”,”five”,”six”, “seven”, “eight”, “nine”};

char *two_digits[] = {“”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”,”fifteen”, “sixteen”,”seventeen”, “eighteen”, “nineteen”};

char *tens_multiple[] = {“”, “”, “twenty”, “thirty”, “forty”, “fifty”,
“sixty”, “seventy”, “eighty”, “ninety”};

char *tens_power[] = {“hundred”, “thousand”};
/* single number*/
if (len == 1) {
printf(“%s\n”, single_digits[*num – ‘0’]);
return;
}

while (*num != ‘\0’) {
if (len >= 3) {
if (*num -‘0’ != 0) {
printf(“%s “, single_digits[*num – ‘0’]);
printf(“%s “, tens_power[len-3]); // here len can be 3 or 4
}
–len;
}

/* Code path for last 2 digits */
else {
if (*num == ‘1’) {
int sum = *num – ‘0’ + *(num + 1)- ‘0’;
printf(“%s\n”, two_digits[sum]);
return;
}
else if (*num == ‘2’ && *(num + 1) == ‘0’) {
printf(“twenty\n”);
return;
}

/* number range 21 to 99 */
else {
int i = *num – ‘0’;
printf(“%s “, i? tens_multiple[i]: “”);
++num;
if (*num != ‘0’)
printf(“%s “, single_digits[*num – ‘0’]);
}
}
++num;
}
}

int main(void)
{
char a[10];
printf(“\nEnter the number : “);
scanf(“%s”,a);
printf(“\nThe number in word is “);
convert(a);
return 0;
}

//This program can be used to convert a number to words

#include <iostream>
#include <string>
using namespace std;

const string EMPTY = “”;

const string X[] = { EMPTY, “One “, “Two “, “Three “, “Four “, “Five “,
“Six “, “Seven “, “Eight “, “Nine “, “Ten “, “Eleven “,
“Twelve “, “Thirteen “, “Fourteen “, “Fifteen “,
“Sixteen “, “Seventeen “, “Eighteen “, “Nineteen ” };

const string Y[] = { EMPTY, EMPTY, “Twenty “, “Thirty “, “Forty “, “Fifty “,
“Sixty “, “Seventy “, “Eighty “, “Ninety ” };

string convert2digit(int n, string suffix)
{
if (n == 0) {
return EMPTY;
}

if (n > 19) {
return Y[n / 10] + X[n % 10] + suffix;
}
else {
return X[n] + suffix;
}
}

string numberToWords(long int n)
{
string res;

res = convert2digit((n % 100), “”);

if (n > 100 && n % 100) {
res = “and ” + res;
}

res = convert2digit(((n / 100) % 10), “Hundred “) + res;

res = convert2digit(((n / 1000) % 100), “Thousand “) + res;

res = convert2digit(((n / 100000) % 100), “Lakh, “) + res;

return res;
}

int main()
{
long int n;
cout <<“\nEnter the number : “;
cin >> n;

cout << numberToWords(n) << ‘\n’;

return 0;
}

//This program can be used to convert a number to words

import java.util.*;
class NumberToWords
{
private static final String EMPTY = “”;

private static final String[] X =
{
EMPTY, “One “, “Two “, “Three “, “Four “, “Five “, “Six “,
“Seven “, “Eight “, “Nine “, “Ten “, “Eleven “,”Twelve “,
“Thirteen “, “Fourteen “, “Fifteen “, “Sixteen “,
“Seventeen “, “Eighteen “, “Nineteen “
};

private static final String[] Y =
{
EMPTY, EMPTY, “Twenty “, “Thirty “, “Forty “, “Fifty “,
“Sixty “, “Seventy “, “Eighty “, “Ninety “
};

private static String convertToDigit(int n, String suffix)
{
if (n == 0) {
return EMPTY;
}

if (n > 19) {
return Y[n / 10] + X[n % 10] + suffix;
}
else {
return X[n] + suffix;
}
}

public static String convert(int n)
{
StringBuilder res = new StringBuilder();

res.append(convertToDigit(((n / 100000) % 100), “Lakh, “));

res.append(convertToDigit(((n / 1000) % 100), “Thousand “));

res.append(convertToDigit(((n / 100) % 10), “Hundred “));

if ((n > 100) && (n % 100 != 0)) {
res.append(“and “);
}

res.append(convertToDigit((n % 100), “”));

return res.toString();
}

public static void main(String[] args)
{
int n;
System.out.println(“\nEnter a number : “);
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println(convert(n));

}
}

Complexity of program to convert words into numbers: O(n)

Output:

number to words