Skip to content
doubtly

2 answers

Best answer

Answer: Find the place value of the first digit (\(10^{d-1}\) for a \(d\)-digit number), then take digits from the left with / and %, printing the word for each digit with a switch.

Logic

  • If the number is 0, print Zero and stop.
  • Find div = place value of the leftmost digit (for 1203, div = 1000).
  • Repeat while div > 0: digit = num / div; print its word; num = num % div; div = div / 10.
  • Taking digits from the left keeps every zero, so 1203 and 100 come out correctly. (Reversing the number first, as some versions do, loses zeros at the end: 120 reversed is 21.)

Program

#include <stdio.h>

void printDigit(int digit);

int main()
{
    int num, temp, div = 1;

    printf("Enter a non-negative integer: ");
    scanf("%d", &num);

    if (num == 0) {
        printDigit(0);
        printf("\n");
        return 0;
    }

    /* div = 10^(number of digits - 1), e.g. 1203 -> 1000 */
    temp = num;
    while (temp >= 10) {
        div = div * 10;
        temp = temp / 10;
    }

    /* take digits from the left, so zeros are not lost */
    while (div > 0) {
        printDigit(num / div);
        num = num % div;
        div = div / 10;
    }
    printf("\n");
    return 0;
}

void printDigit(int digit)
{
    switch (digit) {
        case 0: printf("Zero "); break;
        case 1: printf("One "); break;
        case 2: printf("Two "); break;
        case 3: printf("Three "); break;
        case 4: printf("Four "); break;
        case 5: printf("Five "); break;
        case 6: printf("Six "); break;
        case 7: printf("Seven "); break;
        case 8: printf("Eight "); break;
        case 9: printf("Nine "); break;
    }
}

Sample runs (gcc)

Enter a non-negative integer: 1203
One Two Zero Three
Enter a non-negative integer: 100
One Zero Zero
Enter a non-negative integer: 0
Zero
Doubtly · 20 Jul 2022 Report
Was this answer helpful?

Answer: Find the place value of the first digit, then print each digit from left to right using an array of words.

#include <stdio.h>

int main(void)
{
    const char *words[10] = {"Zero", "One", "Two", "Three", "Four",
                             "Five", "Six", "Seven", "Eight", "Nine"};
    long num, place = 1;

    printf("Enter a positive integer: ");
    scanf("%ld", &num);
    if (num < 0)
        num = -num;

    /* find the place value of the first digit */
    while (num / place >= 10)
        place = place * 10;

    /* print digits from left to right */
    while (place > 0) {
        printf("%s ", words[num / place]);
        num = num % place;
        place = place / 10;
    }
    printf("\n");
    return 0;
}

First place is raised to the place value of the leading digit (1000 for 1020). Then num / place gives each digit from the left, num % place removes it, and place is divided by 10.

Sample outputs: 1020 gives "One Zero Two Zero", 120 gives "One Two Zero", and 0 gives "Zero". The method of reversing the number first fails for numbers ending in 0, because the reversed number drops those zeros.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.