• Write a function digitname that takes a digit and displays its name in uppercase letters; for example if the digit is 4, the function displays FOUR.
  • Write a C program that reads a positive integer n, and calls the above function to display the digits of n as shown in the following above.

Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>

void digitnamev1(int digit)
{
    switch(digit)
    {
        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;
    }
}

void digitnamev2(int digit)
{
    char digits[][10]={"ZERO","ONE","TWO","THREE","FOUR", "FIVE", "SIX", "SEVEN", "EIGHT","NINE"};
    printf("%s" ,digits[digit]);
}
int main()
{
    	int n;
    	do
    	{
        	printf("Enter n >= 0: ");
        	scanf("%d",&n);
    	}while(n<0);
	
	printf("You have entered the value ");
	digitnamev1(n);
	return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Printing a Binary tree