Advertisement
thespeedracer38

Number To Words by Himanshu

Feb 8th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.util.Scanner;
  2. class NumbersToWords
  3. {
  4.     String teens[];
  5.     String ones[];
  6.     String tens[];
  7.     String hundreds[];
  8.     NumbersToWords()
  9.     {
  10.         teens=new String[]{""," ELEVEN ","  TWELVE ","THIRTEEN "," FOURTEEN "," FIFTEEN "," SIXTEEN "," SEVENTEEN "," EIGHTEEN "," NINETEEN "};
  11.         ones=new String[]{"","ONE ","TWO ","THREE ","FOUR "," FIVE ","SIX ","SEVEN ","EIGHT ","NINE ",};    
  12.         tens=new String[]{""," TEN "," TWENTY "," THIRTY "," FORTY "," FIFTY "," SIXTY "," SEVENTY "," EIGHTY "," NININETY "};
  13.         hundreds=new String[]{"","ONE HUNDRED AND ","TWO HUNDRED AND ","THREE HUNDRED AND ","FOUR HUNDRED AND ","FIVE HUNDRED AND ","SIX HUNDRED AND ","SEVEN HUNDRED AND ","EIGHT HUNDRED AND ","NINE HUNDRED AND ",};      
  14.     }
  15.     int counter(int num)
  16.     {
  17.         int ctr=0;
  18.         while(num>0)
  19.         {
  20.             num=num/10;
  21.             ctr++;
  22.         }
  23.         return ctr;
  24.     }
  25.     void numToWords(int num)
  26.     {
  27.         while(num>0)
  28.         {
  29.         String numstr=num+"";
  30.         int ctr=counter(num);
  31.        
  32.        
  33.        
  34.             if(ctr==1)
  35.             {
  36.                 // OLD CODE:
  37.                 //System.out.print(ones[java.lang.Character.getNumericValue(numstr.charAt(0))]);
  38.                 // NEW CODE:
  39.                 System.out.print(ones[numstr.charAt(0) - '0']);
  40.                 break;
  41.             }
  42.             else if (ctr==2)
  43.             {
  44.                 if(numstr.charAt(0)=='1')
  45.                 {
  46.                    
  47.                     // OLD CODE:
  48.                     //System.out.print(teens[java.lang.Character.getNumericValue(numstr.charAt(1))]);
  49.                     // NEW CODE:
  50.                     System.out.print(teens[numstr.charAt(1) - '0']);
  51.                     break;
  52.                 }
  53.                 else
  54.                 {
  55.                     // OLD CODE:
  56.                     //System.out.print(tens[java.lang.Character.getNumericValue(numstr.charAt(0))]);
  57.                     // NEW CODE
  58.                     System.out.print(tens[numstr.charAt(0) - '0']);
  59.                 }
  60.             }
  61.             else if(ctr==3)
  62.             {
  63.                 // OLD CODE:
  64.                 // System.out.print(hundreds[java.lang.Character.getNumericValue(numstr.charAt(0))]);
  65.                 // NEW CODE
  66.                 System.out.print(hundreds[numstr.charAt(0) - '0']);
  67.             }
  68.        
  69.        num=num%(int)Math.pow(10,ctr-1);
  70.    
  71.         }
  72.     }
  73.     public static void main(String args[])
  74.     {
  75.         Scanner sc=new Scanner(System.in);
  76.         NumbersToWords obj=new NumbersToWords();
  77.         System.out.println("Enter the number");
  78.         int num=sc.nextInt();
  79.         obj.numToWords(num);
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement