AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-array: How to convert a number in words?

Please give me a java source code to convert a number into words. Suppose if I enter 245 then output should be two hundred forty five.

corejava x 353
array x 12
Posted On : 2014-07-29 02:25:11.0
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939091
up-rate
5
down-rate

Answers


It can be done by many no. of ways. and the actual concept is firstly store all the low digit numbers upto nineteen like one, two, three, ....... nine, ten, eleven, ..... nineteen in an array. and reset numbers sounds like the combination of 10th position number and one to nine number like(twenty one, thirty two, etc). So now we just need to store all numbers in multiply of 10 after nineteen like(twenty, thirty.... ninety).

Following is the one way to convert the number into words using above concept.

NumberToWord.java:
---------------------------------------

import java.util.*;

public class NumberToWord

{

public void putWord(int n,String ch)

{

String one[]={

" "," one"," two"," three"," four"," five"," six"," seven"," eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};

String ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty"," seventy"," eighty"," ninety"};

if(n>19) { System.out.print(ten[n/10]+" "+one[n%10]);} else { System.out.print(one[n]);}if(n>0)System.out.print(ch);

}

public static void main(String[] args)

{

int n=0;

Scanner s =new Scanner(System.in);

System.out.print("Enter an integer number: ");

n = s.nextInt();

if(n<=0)

System.out.print("Enter numbers greater than 0");

else

{

NumberToWord a =new NumberToWord();

System.out.print("After conversion number in words is :");

a.putWord((n/1000000000)," Hundred");

a.putWord((n/10000000)%100," crore");

a.putWord(((n/100000)%100)," lakh");

a.putWord(((n/1000)%100)," thousand");

a.putWord(((n/100)%10)," hundred");

a.putWord((n%100)," ");

}

}

}

/******************************* Finish Source Code *******************************/

Posted On : 2014-07-29 02:57:55
Satisfied : 4 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129558962
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in