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

后端 未结 8 550
名媛妹妹
名媛妹妹 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 12:07

    import java.lang.*;
    import java.io.*;
    public class rupee
    {
    public static void main(String[] args)throws  IOException
    {
    
    int len=0,revnum=0,i,dup=0,j=0,k=0;
    int gvalue;
     String[] ones={"one","Two","Three","Four","Five","Six","Seven","Eight","Nine","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",""};
    String[] twos={"Ten","Twenty","Thirty","Fourty","fifty","Sixty","Seventy","eighty","Ninety",""};
    System.out.println("\n Enter value");
    InputStreamReader b=new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(b);
    gvalue=Integer.parseInt(br.readLine());
    if(gvalue==10)
    
       System.out.println("Ten");
    
    else if(gvalue==100)
    
      System.out.println("Hundred");
    
    else if(gvalue==1000)
      System.out.println("Thousand");
    
    dup=gvalue;
    for(i=0;dup>0;i++)
    {
      revnum=revnum*10+dup%10;
      len++;
      dup=dup/10;
    }
    while(j10&&gvalue<=19)
     {
      System.out.println(ones[gvalue-2]);
      break;
     }
    else if(gvalue>19&&gvalue<100)
     {
       k=gvalue/10;
       gvalue=gvalue%10;
       System.out.println(twos[k-1]);
     }
    else if(gvalue>100&&gvalue<1000)
     {
       k=gvalue/100;
       gvalue=gvalue%100;
       System.out.println(ones[k-1] +"Hundred");
     }
    else if(gvalue>=1000&&gvalue<9999)
     {
       k=gvalue/1000;
       gvalue=gvalue%1000;
       System.out.println(ones[k-1]+"Thousand");
    }
    else if(gvalue>=11000&&gvalue<=19000)
     {
      k=gvalue/1000;
      gvalue=gvalue%1000;
      System.out.println(twos[k-2]+"Thousand");
     }      
    else if(gvalue>=12000&&gvalue<100000)
    {
     k=gvalue/10000;
         gvalue=gvalue%10000;
     System.out.println(ones[gvalue-1]);
    }
    else
    {
     System.out.println("");
        }
    j++;
    }
    }
    }
    

提交回复
热议问题