Write a program to read an integer from 1 to 999 and print the integer in words. E.g if 897 is read, the program prints eight hundred and ninety-seven.

Source Code

Brief explanation is provided after the source code.

#define MAXWORDLEN 10
#include <stdio.h>
#include <string.h>

void get_dictionary(char a[][MAXWORDLEN]);

int main(int argc, char ** argv) {
    int n, unit, tens, hundredth, key;
    char a[100][MAXWORDLEN];
    get_dictionary(a);
    printf("Enter integer (1-999):\n");
    scanf("%d", &n);

    while (n <= 0 || n > 999) {
        printf("Integer must be between (1-999): ");
        scanf("%d", &n);
    }

    if (n <= 20)
        printf("%s\n", a[n]);

    if ( n > 20 && n < 100) {
        unit = n % 10;
        tens = (n/10)*10;
        printf("%s %s\n", a[tens], a[unit]);
    }

    if ( n >= 100 ){
        int prefix;
        tens = n % 100;
        prefix = n/100;
        if (tens == 0) printf("%s %s\n", a[prefix], a[100]);
        else if (tens > 20) {
            unit = tens % 10;
            tens = (tens/10)*10;
            if (unit == 0) printf("%s %s and %s\n", a[prefix], a[100], a[tens]);
            else
                printf("%s %s and %s-%s\n", a[prefix], a[100], a[tens], a[unit]);
        }
        else
            printf("%s %s and %s\n", a[prefix], a[100], a[tens]);
    }

    return 0;
}

void get_dictionary(char a[][MAXWORDLEN]) {
     
    strcpy(a[0], " "); strcpy(a[1], "one"); strcpy(a[2], "two"); strcpy(a[3], "three");
    strcpy(a[4], "four"); strcpy(a[5], "five"); strcpy(a[6], "six"); strcpy(a[7], "seven");
    strcpy(a[8], "eight"); strcpy(a[9], "nine"); strcpy(a[10], "ten"); strcpy(a[11], "eleven");
    strcpy(a[12], "twele"); strcpy(a[13], "thirteen"); strcpy(a[14], "fourteen"); strcpy(a[15], "fifteen");
    strcpy(a[16], "sixteen"); strcpy(a[17], "seventeen"); strcpy(a[18], "eighteen"); strcpy(a[19], "nineteen");
    strcpy(a[20], "twenty"); strcpy(a[30], "thirty"); strcpy(a[40], "fourty"); strcpy(a[50], "fifty");
    strcpy(a[60], "sixty"); strcpy(a[70], "seventy"); strcpy(a[80], "eighty"); strcpy(a[90], "ninety");
    strcpy(a[100], "hundred");

}

When you compile and execute the above program it produces the following result on Linux:

Enter integer (1-999):
897
eight hundred and ninety-seven

Brief Explanation

  • The program starts by calling the function get_dictionary which initializes the array a with words representation of various integers.
  • Using the printf function, the user is requested to enter an integer between (1-999)
  • Next, a while loop ensures that the correct integer was entered by the user (an integer between 1-999)
  • If the inputted integer is less than or equal to 20, the value stored in the array a at index n is printed to the standard output. So, numbers from 1 to 20 would be printed in words at this level.
  • If the inputted integer is greater than 20 and less than a 100, the unit and the tenth of the number is computed and their respective values in the array a is printed with the tenth first followed by the unit. So, numbers from 21 to 99 would be printed in words at this level.
  • If the inputted integer is greater than or equal to a 100, the tenth of the number is computed followed by the prefix. If the tenth is equal to 0, the value of the array at indexes prefix and 100 is printed to the standard output. If the tenth is greater than 20, the unit of the tenth is computed and if the unit is 0, the value of the array at indexes prefix, 100 and tens is printed but if the unit is not equal to 0, the value of the array at indexes prefix, 100, tens and unit is printed.
  • A return value of zero implies normal termination; non-zero values signal unusual or erroneous termination conditions.
  • NB: The array a is a multidimensional array of size 100. The maximum word length for each index in array a 10.

Add comment


Security code
Refresh