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.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
7
Sample output
Seven
COMPLETE C PROGRAM
Complete C implementation
#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;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Seven
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Associate each integer digit with its word and reject values outside zero through nine.
- 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.
