JAVA Program to Accept Numbers and Display in Words

JAVA Program to Accept Numbers and Display in Words:


import java.io.*;
class num_to_word
{
 public static void main(String args[])
  {
    int n,r,m=0;

  try
  {
    System.out.print("Enter any number : ");
    DataInputStream in = new DataInputStream(System.in);
    n=Integer.parseInt(in.readLine());
    while(n!=0)
    {
        r=n%10;
        m=m*10+r;
        n=n/10;
    }
    n=m;
    while(n!=0)
    {
    r=n%10;

    switch(r)
    {
     case 0:
       System.out.print("Zero ");
       break;
     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 ");
    }
    n=n/10;
    }
   }
     catch(Exception e)
     {
     System.out.println("i/o error");
     }
     System.out.print("\n");
 }
 }

OUTPUT:

Enter any number : 41247
Four One Two Four Seven

Leave a Reply

Your email address will not be published. Required fields are marked *

eighteen + 12 =