Prime #### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime #### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# Convert Digit/Number To Words

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

In C programming, converting a digit or number into 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.
The particular digit is separated in two parts: initial two numbers and last two numbers, after that the two parts are printed individually. ### Working:-

Step 1: Start

Step 2: Get the number from the user smaller than four digits.

Step 3:   If the length is less than 0, print “The String is Empty”.

Step 4: Run the while loop until the number is not equal to zero.

Step 5: Check while the number is less than zero and equal to three, the numbers divided and their corresponding words are written.

Step 6: repeat step 5 until length of the string reaches zero.

Step 7: If length =1, print the words for the single digit.

Step 8: Check conditions if the number lies between 22 and 99 and print the corresponding word.

Step 9: Stop.

### C program:-

```#include<stdio.h>
void convert_to_words(char *num)
{
int l = strlen(num); // Get number of digits in given number
if (l == 0) {
printf(stderr, "empty string\n");
return;
}
if (l > 4) {
printf(stderr, "Length more than 4 is not supported\n");
return;
}

/* The first string is not used, to simplify array indexing */
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"};

/* Used for debugging purpose only */
printf("\n%s: ", num);

/* Single digit number */
if (l == 1) {
printf("%s\n", single_digits[*num - '0']);
return;
}

/* Run while loop until num is not '\0' */
while (*num != '\0') {

/* Code path for first 2 digits */
if (l >= 3) {
if (*num -'0' != 0) {
printf("%s ", single_digits[*num - '0']);
printf("%s ", tens_power[l-3]); // here len can be 3 or 4
}
--l;
}

/* Code path for last 2 digits */
else {
/* Need to handle 10-19 explicitly. 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 handle 20 independently*/
else if (*num == '2' && *(num + 1) == '0') {
printf("twenty\n");
return;
}

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

int main(void)
{
convert_to_words("7784");
convert_to_words("651");
convert_to_words("98");
convert_to_words("9898");

return 0;
}```

### Output

```7784: Seven thousand seven hundred eighty four
651: Six hundred fifty one
98: Ninety Eight
9898: Nine thousand eight hundred ninety eight.```