Program to print the word for each digit present for a given number.

>> Saturday, October 8, 2011

Conver a Number into Words.

Application to print the word for each digit present for a given number.

In this program we have to create two arrays one is ones array with the words of one,two,three.....nineteen.

Then create another array with tens array contains ten,twenty,.....ninety.

Then write the given logic to convert a number into character.

package com.javabynataraj;

public class Num2String {
 
 public static void main(String[] args) {
   
  String ones[] = {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen",};
  String tens[] = {"ten ","twenty ","thirty ","fourty ","fifty ","sixty ","seventy ","eighty ","ninety "};
  
  int n = 7677;
  int i;
  String res=" ";
  
  if(n>1000){
   i=n/1000;
   res=ones[i-1]+" thousand ";
   n=n%1000;
   }
 
  if(n>99 && n<1000){
   i=n/100;
   res=res+ones[i-1]+" hundred ";
   n=n%100;
  }
 
  if(n>19 && n<100){
   i=n/10;
   res=res+tens[i-1];
   n=n%10;
  }
 
  if(n>0 && n<20){
   res=res+ones[n-1];
  }
  System.out.println(res);
 } 
}


Output:


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Google Bookmark Yahoo ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Random Posts

JavabynataraJ - Find me on Bloggers.com Find me on blorner.com Technology Blogs JavabynataraJ Backlinks Blogarama - The Blog Directory

  © Blogger template Webnolia by Ourblogtemplates.com 2009

Back to TOP