Program to convert a given number to words | GeeksforGeeks
Write code to convert a given number into words. For example, if "1234″ is given as input, output should be "one thousand two hundred thirty four".
http://gohired.in/2015/05/convert-number-to-words-java/
Step 1 ) Create array of ones[20] = {“zero”, “one”, “two”, …, “nineteen”};
Step 2) Create array of tens[10] = {“”, “ten”, “twenty”, …, “ninety”};
Step 3) Divide number by 1000, 100, 20 and convert it.
Write code to convert a given number into words. For example, if "1234″ is given as input, output should be "one thousand two hundred thirty four".
void
convert_to_words(
char
*num)
{
int
len =
strlen
(num);
// Get number of digits in given number
/* Base cases */
if
(len == 0) {
fprintf
(stderr,
"empty string\n"
);
return
;
}
if
(len > 4) {
fprintf
(stderr,
"Length more than 4 is not supported\n"
);
return
;
}
/* The first string is not used, it is to make array indexing simple */
char
*single_digits[] = {
"zero"
,
"one"
,
"two"
,
"three"
,
"four"
,
"five"
,
"six"
,
"seven"
,
"eight"
,
"nine"
};
/* The first string is not used, it is to make array indexing simple */
char
*two_digits[] = {
""
,
"ten"
,
"eleven"
,
"twelve"
,
"thirteen"
,
"fourteen"
,
"fifteen"
,
"sixteen"
,
"seventeen"
,
"eighteen"
,
"nineteen"
};
/* The first two string are not used, they are to make array indexing simple*/
char
*tens_multiple[] = {
""
,
""
,
"twenty"
,
"thirty"
,
"forty"
,
"fifty"
,
"sixty"
,
"seventy"
,
"eighty"
,
"ninety"
};
char
*tens_power[] = {
"hundred"
,
"thousand"
};
/* Used for debugging purpose only */
printf
(
"\n%s: "
, num);
/* For single digit number */
if
(len == 1) {
printf
(
"%s\n"
, single_digits[*num -
'0'
]);
return
;
}
/* Iterate while num is not '\0' */
while
(*num !=
'\0'
) {
/* Code path for first 2 digits */
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
{
/* Need to explicitly handle 10-19. Sum of the two digits is
used as index of "two_digits" array of strings */
if
(*num ==
'1'
) {
int
sum = *num -
'0'
+ *(num + 1)-
'0'
;
printf
(
"%s\n"
, two_digits[sum]);
return
;
}
/* Need to explicitely handle 20 */
else
if
(*num ==
'2'
&& *(num + 1) ==
'0'
) {
printf
(
"twenty\n"
);
return
;
}
/* Rest of the two digit numbers i.e., 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;
}
}
Step 1 ) Create array of ones[20] = {“zero”, “one”, “two”, …, “nineteen”};
Step 2) Create array of tens[10] = {“”, “ten”, “twenty”, …, “ninety”};
Step 3) Divide number by 1000, 100, 20 and convert it.
private static final String[] specialNames = { ""," thousand"," million"," billion"," trillion"," quadrillion", " quintillion" }; private static final String[] tensNames = { ""," ten"," twenty"," thirty"," forty"," fifty"," sixty"," seventy", " eighty"," ninety" }; private static final String[] numNames = { ""," one"," two"," three"," four"," five"," six"," seven"," eight", " nine"," ten"," eleven"," twelve"," thirteen"," fourteen"," fifteen", " sixteen"," seventeen"," eighteen"," nineteen" }; private String convertLessThanOneThousand(int number) { String current; if (number % 100 < 20){ current = numNames[number % 100]; number /= 100; } else { current = numNames[number % 10]; number /= 10; current = tensNames[number % 10] + current; number /= 10; } if (number == 0) return current; return numNames[number] + " hundred and" + current; } public String convert(int number) { if (number == 0) { return "zero"; } String prefix = ""; if (number < 0) { number = -number; prefix = "negative"; } String current = ""; int place = 0; do { int n = number % 1000; if (n != 0){ String s = convertLessThanOneThousand(n); current = s + specialNames[place] + current; } place++; number /= 1000; } while (number > 0); return (prefix + current).trim(); }Read full article from Program to convert a given number to words | GeeksforGeeks