# Convert Digit/Number To Words

## Write a C program to find Convert digit/number to words :-

In C programming, converting Digit/number to words requires a specific program. In this, the user is required to insert an integer number which is yet again changed and shown in terms of words with the help of code implementation. The code takes numbers up-to 4 digits, that is, numbers from 0 to 9999. The concept is to make array s that store separate parts of output strings. One array  is intended for single numbers, one for figures from 10 to 19, one for 20, 30, 40, 50, etc, as well as one for powers of 10.

## Algorithm :

• Get the number from the user smaller than four digits.
• If the length is less than 0, print “The String is Empty”.
• Run the while loop until the number is not equal to zero.
• Check while the number is less than zero and equal to three, the numbers divided and their corresponding words are written.
• Repeat step 5 until length of the string reaches zero.
• If length =1, print the words for the single digit.
• Check conditions if the number lies between 22 and 99 and print the corresponding word.

### C program:-

Run
```#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void convert_to_words(char* num)
{
int len = strlen(num);

/* 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;
}

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" };

printf("\n%s: ", num);

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]);
}
--len;
}

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;
}

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)
{
convert_to_words("9459");
return 0;
}```

### Output

`9459 : nine thousand four hundred fifty nine`

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription