Search Tutorials

Loading...

Tuesday, 23 April 2013

Java code to convert a number into words

/*
Program to convert a number into words.
*/
import java.io.*;
class numerals2words
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
numerals2words call = new numerals2words();
System.out.print("Enter a number : ");
int n = Integer.parseInt(br.readLine());
call.convert(n);
}
public void convert(int n)
{
int c;
if(n!=0)
{
c = n%10;
convert(n/10);
num2words(c);
}
}
public void num2words(int n)
{
String words[] =
{"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE
"};
System.out.print(words[n] +" ");
}
}


/**
* ALGORITHM :
* ---------
* 1. Start
* 2. Accept a number from the user.
* 3. Extract a digit from the number.
* 4. Print the digit in words.
* 5. Using recursion repeat steps 3 and 4 till all the digits are
extracted.
* 6. End
*/

/*
OUTPUT :
------
Enter a number : 8496
EIGHT FOUR NINE SIX
*/


1 comment:

Back to Top