Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four)

后端 未结 8 557
名媛妹妹
名媛妹妹 2021-02-01 11:37

Write C/C++/Java code to convert given number into words.

eg:- Input: 1234

Output: One thousand two hundred thirty-four.

Input: 10

Output: Ten

8条回答
  •  清歌不尽
    2021-02-01 11:57

    #include
    #include
    void main()
    {
    int len=0,revnum,i,dup=0,j=0,k=0;
    
     long int gvalue;
    
     char  ones[]  [10]={"one","Two","Three","Four","Five","Six","Seven","Eight","Nine","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",""};
    
     char twos[][10]={"Ten","Twenty","Thirty","Fourty","fifty","Sixty","Seventy","eighty","Ninety",""};
    
    clrscr();
    
    
     printf("\n Enter value"); 
    
     scanf("%ld",&gvalue);
     if(gvalue==10)
    
       printf("Ten");
    
     else if(gvalue==100)
    
       printf("Hundred");
    
     else if(gvalue==1000)
    
       printf("Thousand");
    
     dup=gvalue;
    
     for(i=0;dup>0;i++)
      {
       revnum=revnum*10+dup%10;
    
       len++;
    
       dup=dup/10;
    
      }
    
     while(j10&&gvalue<=19)
    
      {
    
       printf("%s ",ones[gvalue-2]);
    
       break;
    
      }
    
     else if(gvalue>19&&gvalue<100)
    
      {
    
       k=gvalue/10;
    
       gvalue=gvalue%10;
    
        printf("%s ",twos[k-1]);
    
       }
    
      else if(gvalue>100&&gvalue<1000)
    
       {
    
        k=gvalue/100;
    
        gvalue=gvalue%100;
    
        printf("%s Hundred ",ones[k-1]);
    
         }
    
       else if(gvalue>=1000&&gvlaue<9999)
    
       {
    
        k=gvalue/1000;
    
        gvalue=gvalue%1000;
    
        printf("%s Thousand ",ones[k-1]);
    
        }
    
       else if(gvalue>=11000&&gvalue<=19000)
    
        {
    
        k=gvalue/1000;
    
        gvalue=gvalue%1000;
    
        printf("%s Thousand ",twos[k-2]);
    
        }
    
       else if(gvalue>=12000&&gvalue<100000)
    
       {
    
        k=gvalue/10000;
    
        gvalue=gvalue%10000;
    
        printf("%s ",ones[gvalue-1]);
    
       }
    
    else
    
    {
    
    printf("");
    
    }
    
     j++;
    
     getch();
    
    }
    

提交回复
热议问题