Friday, 23 June 2017

java: number(1234) to word(one thousand two hundred and thirty four)

import java.io.*;
import java.util.*;

public class Solution {
    static String out="";
    public static void wordNum(int n,String input){
        String  ones[]={""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"," ten"," eleven"," twelve","                                thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};

        String tens[]={"",""," twenty"," thirty"," forty"," fifty"," sixty","seventy"," eighty"," ninety"};
       
            if(n > 19){
                out+=tens[n/10]+""+ones[n%10];
            }
            else {
                out+=ones[n];
            }
               
             if(n > 0){
                 out+=input;
             }
    }

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        int count=0;
        do{
            int num=sc.nextInt();
            if(num>=0){
                 wordNum((num/1000000000)," Hundred");
                 wordNum((num/10000000)%100," crore");
                 wordNum(((num/100000)%100)," lakh");
                 wordNum(((num/1000)%100)," thousand");
                 wordNum(((num/100)%10)," hundred and");
                 wordNum((num%100),"");
            }
             System.out.println(out.trim());
            out="";
                count++;
                }while(count<=t);
           
        }
    }

No comments:

Post a Comment

java: number(1234) to word(one thousand two hundred and thirty four)

import java.io.*; import java.util.*; public class Solution {     static String out="";     public static void wordNum(int n...