Convert each digit in a number into English. - C Statement

C examples for Statement:switch

Description

Convert each digit in a number into English.

Demo Code

#include <stdio.h>

int main (void)
{
    int input = 123, reversedInput = 0, digit;
    int numberOfDigits = 3; 

    scanf ("%d", &input);

    do{//from   w ww .j  a  va  2 s  .  co m
        reversedInput *= 10;
        reversedInput = reversedInput + input % 10;
        input /= 10;
        numberOfDigits++;

    } while (input != 0);

    do{
        digit = reversedInput % 10;
        reversedInput /= 10;
        numberOfDigits--;

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

    } while (numberOfDigits != 0);

    return 0;
}

Result


Related Tutorials