Q:

C Program to print the two digit number in words

belongs to collection: C Programming on Numbers

0

In this exercise, 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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;
}

Output:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to find all roots of a quadratic equatio... >>
<< C program to find square of a number...