Here's a C program to convert the given number (1 to 10) to characters with output and proper explanation. This program makes use of C's Switch Case and Break statements.
# include <stdio.h> # include <conio.h> void main() { int n ; clrscr() ; printf("Enter a number <1 to 10> : ") ; scanf("%d", &n) ; switch(n) { case 1 : printf("\nONE") ; break ; case 2 : printf("\nTWO") ; break ; case 3 : printf("\nTHREE") ; break ; case 4 : printf("\nFOUR") ; break ; case 5 : printf("\nFIVE") ; break ; case 6 : printf("\nSIX") ; break ; case 7 : printf("\nSEVEN") ; break ; case 8 : printf("\nEIGHT") ; break ; case 9 : printf("\nNINE") ; break ; case 10 : printf("\nTEN") ; break ; default : printf("\nInvalid Input.") ; } getch() ; }
Output of above program is
Enter a number <1 to 10> : 1
ONE
Explanation of above program
The program first asks user to enter a number between 1 to 10. When user enters a number the program take that
value and passes it to the switch-case statement. Here the program picks
a case based on the value that user entered before and executes that
particular case. If no case matches the users request, default case will
be executed.
Suggestion: If you notice you'll see that inside each case there is a break statement. Now what is the need to use break statement inside each case? Read it here - Why use break statement inside switch case.
Suppose user entered 1this value is passed to the switch case statement and the case that matches the value that's passed is executed. In this case, CASE 1 will be executed printing the text "ONE" and then execution of switch case is stopped using a break statement.
When user enters a value that doesn't match any case then the default case is executed.