Converting digits to word format using switch case in C language

Converting digits to word format is a common programming problem that helps understand switch-case statements and number manipulation. In C, we can convert one or two-digit numbers into their English word equivalents using switch statements.

Syntax

switch(expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    default:
        // default code block
}

Example: Converting 1-2 Digit Numbers to Words

This program converts numbers from 0-99 into English words using nested switch statements −

#include <stdio.h>

int main() {
    int no;
    printf("Enter any 1 or 2 digit number: ");
    scanf("%d", &no);
    
    if(no < 0 || no > 99) {
        printf("Invalid number<br>");
    } else {
        printf("Number in words: ");
        
        /* Handle special cases for 0-19 */
        switch(no) {
            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;
            case 10: printf("ten");
                break;
            case 11: printf("eleven");
                break;
            case 12: printf("twelve");
                break;
            case 13: printf("thirteen");
                break;
            case 14: printf("fourteen");
                break;
            case 15: printf("fifteen");
                break;
            case 16: printf("sixteen");
                break;
            case 17: printf("seventeen");
                break;
            case 18: printf("eighteen");
                break;
            case 19: printf("nineteen");
                break;
            default: 
                /* Handle tens place (20-90) */
                switch(no/10) {
                    case 2: printf("twenty");
                        break;
                    case 3: printf("thirty");
                        break;
                    case 4: printf("forty");
                        break;
                    case 5: printf("fifty");
                        break;
                    case 6: printf("sixty");
                        break;
                    case 7: printf("seventy");
                        break;
                    case 8: printf("eighty");
                        break;
                    case 9: printf("ninety");
                        break;
                }
                
                /* Handle ones place for numbers 21-99 */
                switch(no%10) {
                    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;
                }
        }
        printf("<br>");
    }
    return 0;
}
Enter any 1 or 2 digit number: 83
Number in words: eighty three

Enter any 1 or 2 digit number: 6
Number in words: six

Enter any 1 or 2 digit number: 15
Number in words: fifteen

Enter any 1 or 2 digit number: 548
Invalid number

How It Works

  • The program first validates input (0-99 range)
  • Numbers 0-19 have unique names and are handled directly
  • For numbers 20-99, we use no/10 for tens place and no%10 for ones place
  • Nested switch statements handle different digit combinations efficiently

Key Points

  • Special cases (0-19) must be handled separately due to irregular English naming
  • Use break statements to prevent fall-through in switch cases
  • Division (/) and modulus (%) operators help extract individual digits

Conclusion

Switch-case statements provide an elegant solution for digit-to-word conversion. This approach handles special cases and uses mathematical operations to process multi-digit numbers systematically.

Updated on: 2026-03-15T13:43:47+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements