Saturday, 6 June 2015

Conversion of numbers into words in Java

import java.io.*;  
public class num_to_words  
{  
  public static void main(String soham[]) throws Exception  
  {  
    long num,div,num1;  
    int flag=0,digit,position,total_digit;  
  
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  
    System.out.print("\nEnter number: ");  
    num = Integer.parseInt(br.readLine());  
  
    if(num==0)  
    {  
      System.out.print("Zero\n\n");  
    }  
  
    if(num>99999)  
    {  
      System.out.print("please enter number between 0 and 100000\n\n");  
    }  
  
    total_digit = 0;  
    div = 1;  
    num1 = num;  
    while ( num1 > 9 )  
    {  
      num1 = num1 / 10;  
      div = div * 10;  
      total_digit++;  
    }  
      
    total_digit++;  
    position = total_digit;  
    while ( num != 0 )  
    {  
      digit= (int)(num / div);  
      num = num % div;  
      div = div / 10;
        
      switch(position)  
      {  
        case 2:  
        case 5: 
            if ( digit == 1 )
            {
              flag = 1;  
            }
            else  
            {  
              flag = 0;  
              switch(digit)  
              {  
                case 2: System.out.print("twenty ");break;  
                case 3: System.out.print("thirty ");break;  
                case 4: System.out.print("forty ");break;  
                case 5: System.out.print("fifty ");break;  
                case 6: System.out.print("sixty ");break;  
                case 7: System.out.print("seventy ");break;  
                case 8: System.out.print("eighty ");break;  
                case 9: System.out.print("ninty ");  
              }  
            }  
            break;

        case 1:  
        case 4:    
            if ( flag == 1 )  
            {  
              flag = 0;  
              switch(digit)  
              {  
                case 0 : System.out.print("ten ");break;  
                case 1 : System.out.print("eleven ");break;  
                case 2 : System.out.print("twelve ");break;  
                case 3 : System.out.print("thirteen ");break;  
                case 4 : System.out.print("fourteen ");break;  
                case 5 : System.out.print("fifteen ");break;  
                case 6 : System.out.print("sixteen ");break;  
                case 7 : System.out.print("seventeen ");break;  
                case 8 : System.out.print("eighteen ");break;  
                case 9 : System.out.print("ninteen ");  
              }  
            }  
            else  
            {  
              switch(digit)  
              {  
                case 1 : System.out.print("one ");break;  
                case 2 : System.out.print("two ");break;  
                case 3 : System.out.print("three ");break;  
                case 4 : System.out.print("four ");break;  
                case 5 : System.out.print("five ");break;  
                case 6 : System.out.print("six ");break;  
                case 7 : System.out.print("seven ");break;  
                case 8 : System.out.print("eight ");break;  
                case 9 : System.out.print("nine ");  
              }  
            }  
            if ( position == 4 )  
            System.out.print("thousand ");  
            break;

        case 3:  
            if ( digit> 0 )  
            {  
              switch(digit)  
              {  
                case 1 : System.out.print("one ");break;  
                case 2 : System.out.print("two ");break;  
                case 3 : System.out.print("three ");break;  
                case 4 : System.out.print("four ");break;  
                case 5 : System.out.print("five ");break;  
                case 6 : System.out.print("six ");break;  
                case 7 : System.out.print("seven ");break;  
                case 8 : System.out.print("eight ");break;  
                case 9 : System.out.print("nine ");  
              }  
              System.out.print("hundred ");  
            }  
            break;  
      }  
      position--;  
    }  
  
    if ( position == 4 && flag == 0)  
    {  
      System.out.print("thousand");  
    }   
    else  
    {  
      if ( position == 4 && flag == 1)  
      {  
        System.out.print("ten thousand");  
      }  
    }  
      
    if ( position == 1 && flag == 1 )  
    {  
      System.out.print("ten ");  
    }  
      
    System.out.println();  
  }  
}  
Output1:
Enter number: 101
one hundred one

Output2:
Enter number: 1001
one thousand one

No comments:

Post a Comment