C PROGRAMMING PROGRAM • SWITCH & MENU PROGRAMS

Convert a Digit to a Word Using switch

Learn how to convert a digit to a word using switch using a clear C program.

BeginnerswitchInteger casesInput validation

PROBLEM UNDERSTANDING

Input and expected output

Sample input
7
Sample output
Seven

COMPLETE C PROGRAM

Complete C implementation

switch-digit-to-word.c
Open in compiler
#include <stdio.h>

int main(void)
{
    int digit;
    printf("Enter a digit: ");
    scanf("%d", &digit);
    switch (digit) {
        case 0: puts("Zero"); break; case 1: puts("One"); break;
        case 2: puts("Two"); break; case 3: puts("Three"); break;
        case 4: puts("Four"); break; case 5: puts("Five"); break;
        case 6: puts("Six"); break; case 7: puts("Seven"); break;
        case 8: puts("Eight"); break; case 9: puts("Nine"); break;
        default: puts("Input is not a single digit.");
    }
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

Seven
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Associate each integer digit with its word and reject values outside zero through nine.
  3. Display the computed result.

Associate each integer digit with its word and reject values outside zero through nine.

EFFICIENCY

Time and space complexity

Time complexity

O(1)

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.