Converting two digit number to words using switch statement

前端 未结 5 458
说谎
说谎 2021-01-17 04:51

The question says to write a program asking the user to enter 2 digit number, then prints the English word for it. Suppose you enter \'41\' the printf function prints out \'

相关标签:
5条回答
  • 2021-01-17 05:33

    @user2409559 , @Dinever; First,no need of % in ur code as u are taking i/p as first and second digits. Second your code is incomplete. Enter a number b/w 10 to 19 and see the o/p.

    I am posting my code below.......take a look.

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    int num,n,r;
    
    printf("Enter a two-digit number: ");
    scanf("%d",&num);
    
    n = num/10;
    r = num%10;
    
    switch(n)
    { 
        case 1: switch(r)
                {
                    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("Nineteen");
                }
                break;                                    
        case 2: printf("Twenty-");
                break;
        case 3: printf("Thirty-");
                break;
        case 4: printf("Fourty-");
                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;
    }
    if(n != 1)
    {
         switch(r)
         {
             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;
             default: ;                         
    
         }
    }
    getch();
    

    }

    0 讨论(0)
  • 2021-01-17 05:37
    #include <stdio.h>
    
    int main(void)
    {
        int num;
    
        printf("Enter a two digit number: ");
        scanf("%d",&num);
        if(num>10 && num<20){
        badnums(num);
        }
        else{
        goodnums(num);
        }
        return 0;
    }
    
    void goodnums(int num){
    switch(num % 10) {
            case 0: break;
            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(num / 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;
        }
    }
    
    void badnums(int num){
    //handle the numbers here 11-19
    }
    
    0 讨论(0)
  • 2021-01-17 05:42

    Quick and Dirty

    #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;
    }
    
    0 讨论(0)
  • 2021-01-17 05:56

    Try this:

    [0] [ishpeck@yoshimitsu digit]$ cat main.c 
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void teens(int number) 
    {
        switch(number) 
        {
            case 11: printf("eleven"); break;
            case 12: printf("twelve"); break;
            case 13: printf("thirteen"); break;
            case 14: printf("fourteen"); break;
            case 15: printf("fifteen"); break;
            case 16: printf("sixteen"); break;
            case 17: printf("seventeen"); break;
            case 18: printf("eighteen"); break;
            case 19: printf("nineteen"); break;
            default: printf("ten");
        }
    }
    
    void donums(int number) 
    {
        switch(number/10) 
        {
            case 1: return teens(number);
            case 2: printf("twenty"); break;
            case 3: printf("thirt"); break;
            case 4: printf("fourt"); break;
            case 5: printf("fift"); break;
            case 6: printf("sixt"); break;
            case 7: printf("sevent"); break;
            case 8: printf("eighty"); break;
            case 9: printf("ninety"); break;
        }
        switch(number%10)
        {
            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;
        }
    }
    
    int main(void)
    {
        int number;
        printf("Enter two digit number.\n");
        scanf("%d", &number);
        donums(number);
        printf("\n");
        return 0;
    }
    [0] [ishpeck@yoshimitsu digit]$ gcc --version
    gcc (GCC) 4.7.1
    Copyright (C) 2012 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    [0] [ishpeck@yoshimitsu digit]$ gcc -o digits main.c
    [0] [ishpeck@yoshimitsu digit]$ for x in {10..99}; do echo $x | ./digits; done
    Enter two digit number.
    ten
    Enter two digit number.
    eleven
    Enter two digit number.
    twelve
    Enter two digit number.
    thirteen
    Enter two digit number.
    fourteen
    Enter two digit number.
    fifteen
    Enter two digit number.
    sixteen
    Enter two digit number.
    seventeen
    Enter two digit number.
    eighteen
    Enter two digit number.
    nineteen
    Enter two digit number.
    twenty
    Enter two digit number.
    twenty one
    Enter two digit number.
    twenty two
    Enter two digit number.
    twenty three
    Enter two digit number.
    twenty four
    Enter two digit number.
    twenty five
    Enter two digit number.
    twenty six
    Enter two digit number.
    twenty seven
    Enter two digit number.
    twenty eight
    Enter two digit number.
    twenty nine
    Enter two digit number.
    thirt
    Enter two digit number.
    thirt one
    Enter two digit number.
    thirt two
    Enter two digit number.
    thirt three
    Enter two digit number.
    thirt four
    Enter two digit number.
    thirt five
    Enter two digit number.
    thirt six
    Enter two digit number.
    thirt seven
    Enter two digit number.
    thirt eight
    Enter two digit number.
    thirt nine
    Enter two digit number.
    fourt
    Enter two digit number.
    fourt one
    Enter two digit number.
    fourt two
    Enter two digit number.
    fourt three
    Enter two digit number.
    fourt four
    Enter two digit number.
    fourt five
    Enter two digit number.
    fourt six
    Enter two digit number.
    fourt seven
    Enter two digit number.
    fourt eight
    Enter two digit number.
    fourt nine
    Enter two digit number.
    fift
    Enter two digit number.
    fift one
    Enter two digit number.
    fift two
    Enter two digit number.
    fift three
    Enter two digit number.
    fift four
    Enter two digit number.
    fift five
    Enter two digit number.
    fift six
    Enter two digit number.
    fift seven
    Enter two digit number.
    fift eight
    Enter two digit number.
    fift nine
    Enter two digit number.
    sixt
    Enter two digit number.
    sixt one
    Enter two digit number.
    sixt two
    Enter two digit number.
    sixt three
    Enter two digit number.
    sixt four
    Enter two digit number.
    sixt five
    Enter two digit number.
    sixt six
    Enter two digit number.
    sixt seven
    Enter two digit number.
    sixt eight
    Enter two digit number.
    sixt nine
    Enter two digit number.
    sevent
    Enter two digit number.
    sevent one
    Enter two digit number.
    sevent two
    Enter two digit number.
    sevent three
    Enter two digit number.
    sevent four
    Enter two digit number.
    sevent five
    Enter two digit number.
    sevent six
    Enter two digit number.
    sevent seven
    Enter two digit number.
    sevent eight
    Enter two digit number.
    sevent nine
    Enter two digit number.
    eighty
    Enter two digit number.
    eighty one
    Enter two digit number.
    eighty two
    Enter two digit number.
    eighty three
    Enter two digit number.
    eighty four
    Enter two digit number.
    eighty five
    Enter two digit number.
    eighty six
    Enter two digit number.
    eighty seven
    Enter two digit number.
    eighty eight
    Enter two digit number.
    eighty nine
    Enter two digit number.
    ninety
    Enter two digit number.
    ninety one
    Enter two digit number.
    ninety two
    Enter two digit number.
    ninety three
    Enter two digit number.
    ninety four
    Enter two digit number.
    ninety five
    Enter two digit number.
    ninety six
    Enter two digit number.
    ninety seven
    Enter two digit number.
    ninety eight
    Enter two digit number.
    ninety nine
    
    0 讨论(0)
  • 2021-01-17 05:57
    int main(void)
    {
        int first_digit, second_digit;
    
        printf("Enter two digits: ");
        scanf("%1d%1d",&first_digit,&second_digit);
    
    
        switch(first_digit % 10) {
            case 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;
            }
            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;
    }
    
    0 讨论(0)
提交回复
热议问题