What if someone asks you a java program for writing numbers in words .The problem with the question is that we dont know how big the input number can be. So first question we need to ask what would be the range of the input. After getting the range , we can answer through algorithm or code of program.We provided two answers to this question as the countries like USA, UK uses million,billion,trillion terminology while Asian countries like India,Pakistan ,Srilanka uses lakh,crore,arab,kharab terminology.So lets find out the logic and java program for writing numbers in words.
Read Also : Length of String without using length() method
Convert String to int without using parseInt() method
There are two type of word formation depending upon the geography
Type 1 : in countries like United states of America(USA)
The format for writing numbers in words is like this
Unit,ten,hundred,thousand,million,billion,trillion,quadrillion,quintillion.
For example :
Input : 123456789
Output: One hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
Logic :
After thousand number , there is a symmetry in special names like million,billion,trillion,quadrillion,quintillion.
The symmetry is if you multiply 1000 with thousand than we will get a million. If we multiply 1000 with million then we will get billion. If we multiply 1000 with billion then we get trillion.Similarly,1000 multiply trillion equals quadrillion. 1000 multiply quadrillion equals quintillion.This symmetry starts after we reach 1000 number . So we will divide the program into two parts .
First part that is function convertLessThanOneThousand(int number) will convert any number smaller than 1000 into words.
Second part, starting from extreme-right of input number, we will use modulus operator by1000, to get the last three extreme right digits of the input number.Taking three digits at a time from right to left , we will scan the whole input number until it is fully converted into the word.
Java Code for writing Numbers in Words :
Read Also : How to generate Random Number in Java
Top 15 must prepare Behavioral Interview Questions
Type 2 : in Asian countries like India, Pakistan, Srilanka
The format for writing numbers in words is like this
Unit,ten,hundred,thousand,lakh,crore,arab,kharab
Input : 123456789
Output: Twelve crore thirty four lakhs fifty six thousand seven hundred eighty nine
Logic :
In this number to word conversion , there is hundred pattern instead of thousand pattern mentioned above.
So, after reaching thousand , the pattern is 100 multiply thousand is lakh. 100 multiply lakh is crore . 100 multiply crore is arab. 100 multiply arab is kharab and so on. You can solve this question by above method also.
Java Code for writing Numbers in Words :
Read Also : Length of String without using length() method
Convert String to int without using parseInt() method
There are two type of word formation depending upon the geography
Type 1 : in countries like United states of America(USA)
The format for writing numbers in words is like this
Unit,ten,hundred,thousand,million,billion,trillion,quadrillion,quintillion.
For example :
Input : 123456789
Output: One hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
Logic :
After thousand number , there is a symmetry in special names like million,billion,trillion,quadrillion,quintillion.
The symmetry is if you multiply 1000 with thousand than we will get a million. If we multiply 1000 with million then we will get billion. If we multiply 1000 with billion then we get trillion.Similarly,1000 multiply trillion equals quadrillion. 1000 multiply quadrillion equals quintillion.This symmetry starts after we reach 1000 number . So we will divide the program into two parts .
First part that is function convertLessThanOneThousand(int number) will convert any number smaller than 1000 into words.
Second part, starting from extreme-right of input number, we will use modulus operator by1000, to get the last three extreme right digits of the input number.Taking three digits at a time from right to left , we will scan the whole input number until it is fully converted into the word.
Java Code for writing Numbers in Words :
public class NumberToWord
{ private static final String[] specialNames = { "", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion" }; private static final String[] tensNames = { "", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" }; private static final String[] numNames = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" }; private String convertLessThanOneThousand(int number) { String current; if (number % 100 < 20){ current = numNames[number % 100]; number /= 100; } else { current = numNames[number % 10]; number /= 10; current = tensNames[number % 10] + current; number /= 10; } if (number == 0) return current; return numNames[number] + " hundred" + current; } public String convert(int number) { if (number == 0) { return "zero"; } String prefix = ""; if (number < 0) { number = -number; prefix = "negative"; } String current = ""; int place = 0; do { int n = number % 1000; if (n != 0){ String s = convertLessThanOneThousand(n); current = s + specialNames[place] + current; } place++; number /= 1000; } while (number > 0); return (prefix + current).trim(); } public static void main(String[] args) { NumberToWord obj = new NumberToWord(); System.out.println("*** " + obj.convert(123456789)); System.out.println("*** " + obj.convert(-55)); } }
one hundred twenty three million four hundred fifty six thousand seven hundred
eighty nine negative fifty five
Read Also : How to generate Random Number in Java
Top 15 must prepare Behavioral Interview Questions
Type 2 : in Asian countries like India, Pakistan, Srilanka
The format for writing numbers in words is like this
Unit,ten,hundred,thousand,lakh,crore,arab,kharab
Input : 123456789
Output: Twelve crore thirty four lakhs fifty six thousand seven hundred eighty nine
Logic :
In this number to word conversion , there is hundred pattern instead of thousand pattern mentioned above.
So, after reaching thousand , the pattern is 100 multiply thousand is lakh. 100 multiply lakh is crore . 100 multiply crore is arab. 100 multiply arab is kharab and so on. You can solve this question by above method also.
Java Code for writing Numbers in Words :
import java.util.*;
public class NumberToWord
{ private static String input; private static int num; private static String[] units=
{"", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine" }; private static String[] teen= {" Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen" }; private static String[] tens= { " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety" }; private static String[] maxs= {"", "", " Hundred", " Thousand", " Lakh", " Crore" };
public String convertNumberToWords(int n) { input=numToString(n);
String converted="";
int pos=1;
boolean hun=false; while(input.length()> 0) { if(pos==1) // TENS AND UNIT POSITION { if(input.length()>= 2) // TWO DIGIT NUMBERS {
String temp=input.substring(input.length()-2,input.length()); input=input.substring(0,input.length()-2); converted+=digits(temp); } else if(input.length()==1) // 1 DIGIT NUMBER { converted+=digits(input);
input=""; } pos++; } else if(pos==2) // HUNDRED POSITION {
String temp=input.substring(input.length()-1,input.length()); input=input.substring(0,input.length()-1); if(converted.length()> 0&&digits(temp)!="") { converted=(digits(temp)+maxs[pos]+" and")+converted;
hun=true; } else { if (digits(temp)==""); else converted=(digits(temp)+maxs[pos])+converted;hun=true; } pos++; } else if(pos > 2) // REMAINING NUMBERS PAIRED BY TWO { if(input.length()>= 2) // EXTRACT 2 DIGITS {
String temp=input.substring(input.length()-2,input.length()); input=input.substring(0,input.length()-2); if(!hun&&converted.length()> 0) converted=digits(temp)+maxs[pos]+" and"+converted; else { if(digits(temp)=="") ; else converted=digits(temp)+maxs[pos]+converted; } } else if(input.length()==1) // EXTRACT 1 DIGIT {
if(!hun&&converted.length()> 0) converted=digits(input)+maxs[pos]+" and"+converted; else { if(digits(input)=="") ; else converted=digits(input)+maxs[pos]+converted; input=""; } } pos++; } } return converted; } private String digits(String temp) // TO RETURN SELECTED NUMBERS IN WORDS { String converted=""; for(int i=temp.length()-1;i >= 0;i--) { int ch=temp.charAt(i)-48; if(i==0&&ch>1 && temp.length()> 1) converted=tens[ch-2]+converted; // IF TENS DIGIT STARTS WITH 2 OR MORE IT FALLS UNDER TENS else if(i==0&&ch==1&&temp.length()==2) // IF TENS DIGIT STARTS WITH 1 IT FALLS UNDER TEENS { int sum=0; for(int j=0;j < 2;j++) sum=(sum*10)+(temp.charAt(j)-48); return teen[sum-10]; } else { if(ch > 0) converted=units[ch]+converted; } // IF SINGLE DIGIT PROVIDED } return converted; } private String numToString(int x) // CONVERT THE NUMBER TO STRING { String num=""; while(x!=0) { num=((char)((x%10)+48))+num; x/=10; } return num; } private void inputNumber() { Scanner in=new Scanner(System.in); try
{
System.out.print("Please enter number to Convert into Words : "); num=in.nextInt();
}
catch(Exception e)
{
System.out.println("Number should be Less than 1 Arab ");
System.exit(1);
} } public static void main(String[] args) { NumberToWord obj=new NumberToWord(); obj.inputNumber();
System.out.println("input in Words : "+obj.convertNumberToWords(num)); } }
Please mention in the comments in case you have any other simple way or any doubts regarding code .