C program to convert digit to words

In C programming, converting a digit to its corresponding word representation is a common task. This involves taking a single digit (0-9) and converting it to its English word equivalent like "Zero", "One", "Two", etc.

Syntax

void digitToWord(int digit);

Method 1: Using If-Else Statements

This approach uses a series of if-else statements to check each digit and print the corresponding word −

#include <stdio.h>

void digitToWord(int d) {
    if (d < 0 || d > 9) {
        printf("Beyond range of 0 - 9");
    } else if (d == 0) {
        printf("Zero");
    } else if (d == 1) {
        printf("One");
    } else if (d == 2) {
        printf("Two");
    } else if (d == 3) {
        printf("Three");
    } else if (d == 4) {
        printf("Four");
    } else if (d == 5) {
        printf("Five");
    } else if (d == 6) {
        printf("Six");
    } else if (d == 7) {
        printf("Seven");
    } else if (d == 8) {
        printf("Eight");
    } else if (d == 9) {
        printf("Nine");
    }
}

int main() {
    int d = 6;
    printf("Input: %d<br>", d);
    printf("Output: ");
    digitToWord(d);
    printf("<br>");
    return 0;
}
Input: 6
Output: Six

Method 2: Using Switch Statement

A cleaner approach using switch-case statement for better readability −

#include <stdio.h>

void digitToWordSwitch(int d) {
    switch (d) {
        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;
        default: printf("Beyond range of 0 - 9"); break;
    }
}

int main() {
    int digits[] = {3, 7, 15, -2};
    int size = sizeof(digits) / sizeof(digits[0]);
    
    for (int i = 0; i < size; i++) {
        printf("Digit: %d -> ", digits[i]);
        digitToWordSwitch(digits[i]);
        printf("<br>");
    }
    return 0;
}
Digit: 3 -> Three
Digit: 7 -> Seven
Digit: 15 -> Beyond range of 0 - 9
Digit: -2 -> Beyond range of 0 - 9

Method 3: Using Array of Strings

The most efficient approach using an array to store digit words −

#include <stdio.h>

void digitToWordArray(int d) {
    char *words[] = {"Zero", "One", "Two", "Three", "Four", 
                     "Five", "Six", "Seven", "Eight", "Nine"};
    
    if (d >= 0 && d <= 9) {
        printf("%s", words[d]);
    } else {
        printf("Beyond range of 0 - 9");
    }
}

int main() {
    printf("Testing array method:<br>");
    for (int i = 0; i <= 9; i++) {
        printf("%d: ", i);
        digitToWordArray(i);
        printf("<br>");
    }
    return 0;
}
Testing array method:
0: Zero
1: One
2: Two
3: Three
4: Four
5: Five
6: Six
7: Seven
8: Eight
9: Nine

Key Points

  • Always validate input range (0-9) before conversion.
  • Switch statement provides cleaner code than multiple if-else statements.
  • Array method is most efficient for lookup operations.
  • Handle edge cases for invalid inputs appropriately.

Conclusion

Converting digits to words in C can be implemented using if-else, switch, or array methods. The array approach is most efficient and scalable for larger digit ranges.

Updated on: 2026-03-15T14:22:53+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements