C Program to print the two digit number in words

In this blog post, we learn how to write a C Program to print the two digit number in words?. We will write the C Program to print the two digit number in words using switch cases. How to display number in words using loop in C programming. Write a C program to input a two digit number from the user and print it into words using for loop. Logic to print two digit number in words in C programming.

Example,

Input number: 12

Output: twelve

 

 

C Program to print the two digit number in words:

The program consists of two outer switch statements. The first switch statement prints the word for the first digit and the second switch case statement print the word for the second digit.

In first switch case, we have used another nested switch case to handle the numbers from 11 to 19 because require some trick to print these numbers.

#include <stdio.h>

int main(void)
{
    int firstDigit, secondDigit;

    printf("Enter a two-digit number: ");
    scanf("%1d%1d", &firstDigit, &secondDigit);

    printf("You have entered: ");

    // print word for the first digit
    switch (firstDigit)
    {
    case 1:
        // special case for numbers between 11-19
        switch (secondDigit)
        {
        case 0:
            printf("ten");
            return 0;
        case 1:
            printf("eleven");
            return 0;
        case 2:
            printf("twelve");
            return 0;
        case 3:
            printf("thirteen");
            return 0;
        case 4:
            printf("fourteen");
            return 0;
        case 5:
            printf("fifteen");
            return 0;
        case 6:
            printf("sixteen");
            return 0;
        case 7:
            printf("seventeen");
            return 0;
        case 8:
            printf("eigthteen");
            return 0;
        case 9:
            printf("nineteen");
            return 0;
        }
    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;
    }

    // print word for the second digit
    switch (secondDigit)
    {
    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;
    }

    return 0;
}

Output1:

Enter a two-digit number: 11
You have entered: eleven

 

Output 2:

Enter a two-digit number: 67
You have entered: sixty-seven

Output 3:

Enter a two-digit number: 99
You have entered: ninety-nine

 

Using a combination of if statement and switch case you can also print two-digit numbers in words. Let see the code,

#include <stdio.h>

int main(void)
{
    int first_digit, second_digit;

    printf("Enter two digits: ");
    scanf("%1d%1d",&first_digit,&second_digit);

    if (first_digit == 1)
    {
        switch(second_digit % 10)
        {
        case 0:
            printf(" ten");
            break;
        case 1:
            printf(" eleven");
            break;
        case 2:
            printf(" twelve");
            break;
        case 3:
            printf(" thirteen");
            break;
        case 4:
            printf(" fourteen");
            break;
        case 5:
            printf(" fifteen");
            break;
        case 6:
            printf(" sixteen");
            break;
        case 7:
            printf(" seventeen");
            break;
        case 8:
            printf(" eighteen");
            break;
        case 9:
            printf(" ninteen");
            break;
        }
        return 0;
    }
    switch(first_digit % 10)
    {
    case 1:
        printf("ten");
        break;
    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;
    }
    switch(second_digit % 10)
    {
    case 0:
        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;
    }
    return 0;
}

Output :

Enter a two-digit number: 67
You have entered: sixty-seven

Leave a Reply

Your email address will not be published. Required fields are marked *