Converting two digit number to words using switch statement

前端 未结 5 460
说谎
说谎 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 
    #include 
    
    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();
    

    }

提交回复
热议问题