JAVA Coding
Have to write a function that will write out from 1 to 10000 in words.
For example if i give "1" as input the output should be "one"
View Answers
August 4, 2010 at 12:41 PM
Hi Friend,
Try the following code:
public class NumToWords {
String string;
String st1[]={"","one","two","three","four","five","six","seven","eight","nine",};
String st2[]={"hundred","thousand","lakh","crore"};
String st3[]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen",};
String st4[]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
public String convert(int number){
int n=1;
int word ;
string="";
while ( number != 0 ){
switch ( n ){
case 1 :
word = number % 100 ;
pass ( word ) ;
if( number > 100 && number % 100 != 0 ){
show ( "and " ) ;
}
number /= 100 ;
break ;
case 2 :
word = number % 10 ;
if ( word != 0 ){
show ( " " ) ;
show ( st2[0] ) ;
show ( " " ) ;
pass ( word ) ;
}
number /= 10 ;
break ;
case 3 :
word = number % 100 ;
if ( word != 0 ){
show( " " ) ;
show( st2[1] ) ;
show( " " ) ;
pass( word ) ;
}
number /= 100 ;
break ;
case 4 :
word = number % 100 ;
if ( word != 0 ){
show ( " " ) ;
show ( st2[2] ) ;
show ( " " ) ;
pass ( word ) ;
}
number /= 100 ;
break ;
case 5 :
word = number % 100 ;
if ( word != 0 ){
show ( " " ) ;
show ( st2[3] ) ;
show ( " " ) ;
pass ( word ) ;
}
number /= 100 ;
break ;
}
n++ ;
}
return string;
}
public void pass(int number){
int word, q ;
if ( number < 10 ){
show ( st1[number] ) ;
}
if ( number > 9 && number < 20 ){
show ( st3[number-10] ) ;
}
if ( number > 19 ){
word = number % 10 ;
if ( word == 0 ){
q = number / 10 ;
show ( st4[q-2] ) ;
}
else{
q = number / 10 ;
show ( st1[word] ) ;
show ( " " ) ;
show ( st4[q-2] ) ;
}
}
}
public void show(String s){
String st ;
st= string ;
string= s ;
string+= st ;
}
public static void main(String[] args) {
NumToWords w=new NumToWords();
for(int i=1;i<=10000;i++){
String inwords=w.convert(i);
System.out.println(inwords);
}
}
}
Thanks
Related Tutorials/Questions & Answers:
Java CodingJava Coding Hello, Can u please tel me how to improve my
coding techniques.I am feeling difficulty with
coding but I am perfect with my theory part
coding in javacoding in java write a code to calculate the product of odd integers from 1-15
class ProductOfOddIntegers
{
public static void main(String[] args)
{
long product=1;
for(int i=1;i<=15;i
Advertisements
java codingjava coding Hi sir,
WRITE A PROGRAMME TO MAKE EMPLOYMENT MANAGMENT HAVING COLORS AND FRAME IN IT,????
CAN U SUGGEST ME HOW TO MAKE IT IN CORE
JAVA java codingjava coding sir i need code for how to send meg from one system to another system
java codingjava coding i need to to a project in
java and the title is file parsing. i need to read a file or if a particular file is given i have to count the no of vowels, pick out some terms, and to display the details about that file
java codingjava coding i am using netbean to my project and it is Desktop Application.i want my textfield to accept only numbers or only alphabets .........plez plez plez do help me..........i am just beginner to
java codes
java codingjava coding Hai,I am having order table in mysql database.when user click download button the order table data shows in csv format.Its very urgent.please help me.
Thanks in advance
java codingjava coding i have source code for my project virtual classroom. i need a little guidance to run that code..do help me out
java codingjava coding using swing i want to give an exit button which on clicking ask a msg "JOptionPane" is select "yes" then it should exit or else not....plez help me......
JOptionPane.showConfirmDialog(null,"DO YOU REALLY WANT
JAVA codingJAVA coding using classes and arrays implement the following create classes, inheritance, object and execute methods.
there are 5 students in a class, capture the surname, first name, age and sex.
capture their marks of a given
java codingjava coding plz correct this one ....i want to retrive data row from databse ...all its working but for combobox it not.......plz plz plz help me......
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt
java codingjava coding (person, student,employee, faculty and staff are classes) design a class named person and its two subclasses named student and employee
class Person {
public String name;
public String address;
public
java codingjava coding can any one plez tell me how to retrive a row from access to each field of swing...
i hav tried this but i am getting an SQL error "SQLExceptionOccurred[Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect
Image using Java codingImage using
Java coding Hai,
Display image in pdf file using
Java coding through Xsl file.. Please help me.. xsl file generate the pdf file
coding - Java Beginnerscoding i need to start
coding but i don't know how to start it so plz help me Hi Friend,
Please visit the following link:
http://www.roseindia.net/
java/
Thanks
color coding in javacolor
coding in java hii..can anyone please help with auto coloring code in
java swings..??a particular text field should have a particular color for a particular event..plzz help
single frame coding in javasingle frame
coding in java sir,
i am a beginner in java.i want to do a desktop application in
java in which a single frame satisfies all needs.but i want changes in that frame only.how can i solve this?i think multiple frames
java coding - Java Beginnersjava coding dear sir,
i'm doing a
java coding using with netbeans ide, so i'm trying to pass some text from one jframe to another jframe which is both are already opend on screen then how to do it?
please send me
Coding - Java BeginnersCoding How to do a
coding to view employee name, salary and EPF. The EPF is calculated from the salary like 10% from their salary. and it also show... the technologies you create the application in
Java/JSP/Servlet/JSF/Struts
coding problem - Java Beginnerscoding problem hi friend!
Im new to jasper reports.how can i start that
coding inorder to generate reports.can u send some sample programs for reporting?im badly need some clearly mentioned example because im new to jasper
j2me coding - Java Magazine in sms using j2me.
i have a problem in
coding for sending and receiving sms .i have a
coding for
receiving sms but it not work because that program need sending... a
coding for sending and receiving sms in j2me
coding related - Java Beginnerscoding related what is the purpose of using these line in a program?
char units = args[1].charAt(0);
units = Character.toUpperCase.../
java/
java-conversion
coding problem - Java Beginnerscoding problem hi! i declared date as date datatype in oracle now i need to retrieve date from database to my
java code.how can i please help me urgent Hi Friend,
We are providing you a code that will retrieve
Plz give java coding for thisPlz give
java coding for this ****** *
* *
***********
* *
* ******
Hello Friend;
below is the code :
public class Pattern {
public static void main(String[] args) {
int
java coding help plz ?java coding help plz ? Given two integers N and M (N ΓΆβ?°Β¤ M), output all the prime numbers between N and M inclusive, one per line.
N and M will be positive integers less than or equal to 1,000,000,000.
The difference
coding problem - Java Beginnerscoding problem i had a big doubt.
public ArrayList readExpenseType() throws ClassNotFoundException, SQLException,Exception
{
DBConnection dbc = new DBConnection();
Connection con = dbc.getConnection();
ArrayList
codingcoding I need the logout
coding. can you please help me.
Please visit the following links:
http://www.roseindia.net/quickguide/tomcat/Logout.shtml
http://www.roseindia.net/jsp/loginstatus.shtml
coding problem - Java Beginnerscoding problem i used netbeans software to develop frame.now i need to add values to JList from ArrayList object Hi Friend,
Try the following code:
import java.util.*;
import javax.swing.*;
public class
Java Coding - Java BeginnersJava Coding Code a for loop that adds the integers that are multiples of 5 starting with 50 and ending with 5. Each time the sum is increased, print it on a line at the console. Be sure to declare and initialize any required
Java Coding - Java BeginnersJava Coding What is the meaning of following statement in
Java?
Clasname objectname = another_object_name.method_name;
The Example Code is Given Below
public void sessionCreated(HttpSessionEvent sessionEvent
Java Coding - Java BeginnersJava Coding How do I code the following:
Code a while loop that calculates the sum of the integers that are multiples of 5 starting with 5... on
Java visit to :
http://www.roseindia.net/
java/
Thanks
Java Coding - Java BeginnersJava Coding Code nested for loops that calculate the area of a rectangle that can range in size from a length and width of 1 to a length and width of 10. Print the area for each width for a given length on a single line
Java Coding - Java BeginnersJava Coding Code a private static method named printMessage that accepts a String parameter that represents the message. The method should simply print the message followed by a blank line at the console.
Write a statement
Java Coding - Java BeginnersJava Coding Two overloading methods that returns average using following headers
A) public static int average(int[] array)
B) public static double average(double[] array
The program should prompt the user to enter
Java Coding - Java BeginnersJava Coding How do I code the following:
Code a switch statement that tests the value of an int variable named weightInt. This variable contains the weight of a shipment rounded up to the nearest multiple of 5. If the value
Java Coding - Java BeginnersJava Coding How do I code the following:
Code a private static method named getUPSShipping that accepts a double value named weight and an integer named zipCode and returns a double value. This method should set the value
JAVA Coding - Java BeginnersJAVA Coding Have to write a function that will write out from 1...,
q ;
if ( number < 10 ){
show ( st1[number] ) ;
}
if ( number > 9... = number % 10 ;
if ( word == 0 ){
q = number / 10 ;
show ( st4[q-2] ) ;
}
else
Java Coding - Java BeginnersJava Coding Write a program containing two functions, bubbleSort( ) and mergeSort( ). Each function should take an array of integers, sort the array using the appropriate algorithm and then return the sorted array
Java Coding - Java BeginnersJava Coding How do I code the following?
Code an if/else statement that tests the value of a string variable named shippingMethod to determine if it has a value of ?UPS,? ?FEDEX,? or another value. If the value is ?UPS,? call
JAVA Coding - Java BeginnersJAVA Coding Have to write a function that will write out from 1... word,
q ;
if ( number < 10 ){
show ( st1[number] ) ;
}
if ( number > 9... ){
word = number % 10 ;
if ( word == 0 ){
q = number / 10 ;
show ( st4[q-2
Java Coding - Java BeginnersJava Coding How do I do the following:
Code an if/else statement that tests the value of a string variable named shippingMethod to determine if it has a value of ?UPS,? ?FEDEX,? or another value. If the value is ?UPS,? code
java coding - JSP-Servletjava coding how to send mail in LAN without internet using
java progamme Hi Friend,
Please visit the following link:
http://www.roseindia.net/javamail/
Thanks
need of java coding - JavaMailneed of
java coding Design a
java interface for ADT stack. Develop it and implement the interface using link list. Provide necessary exception handling for that implementations.
pls mail me this lab program..its urgent
Java Coding - Date CalendarJava Coding Write a program that prompts the user to enter the year and first day of the year and displays the calendar for the year on the console. For example,if the user entered the year 2005, and 6 for Saturday, January
Browser making by java codingBrowser making by
java coding import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Browser extends JFrame implements
codingcoding Write a
java program to Create table - Employee containing empId String (20), empName String (50), DOB Date, deptId String(20). Primary key is empId. Foreign Key is deptId. Populate some records