- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Enter a Number in Digit and Display in Words
Digits are basically represented in number format or these are integer values. But for pronouncing them we are using words. Every digit has a unique word format.
For example, the word format of 1 is “One”. Like that for 2 the word format is “two”, for 3 the word format is “three” ... etc. For two-digit number the number format is little different. 21 represents as “twenty-one”, 45 represents as “forty-five” .... etc. So for all type of number there has a unique word format available.
To show you some instances
Instance-1
Input number is 15. Word format of 15 = Fifteen
Instance-2
Input number is 123. Word format of 123 = One Hundred Twenty-Three
Instance-3
Input number is 4532. Word format of 4532 = Four Thousand Five Hundred Thirty-Two.
Algorithm
Step-1 − Get the input number by static input method.
Step-2 − Declare an array of string type and store the number in string.
Step-3 − Then we are declaring some other string type of array in which we store the word format of different type of digits like one digit number, two-digit number, hundred place and thousand place (Here we are only written code up to four-digit number which you can extend further as per your requirement).
Step-4 − We are finding the length of the given number. According to the length of the number, we have different conditions to concatenate the array’s index value.
Step-5 − After completing the process we print that number along with its word format.
Approach
We have provided the solution in different approaches.
By User Defined Method with Static Input Values.
Let’s see the program along with its output one by one.
Approach − By Using User Defined Method with Static Input Value
In this approach, we declare some random input numbers by static input and pass these numbers as parameters in a user defined method, then inside the method by using the algorithm we can print the word format of given number.
Example
public class Main{ public static void main(String args[]) { digitToWord("1234".toCharArray()); digitToWord("557".toCharArray()); digitToWord("45".toCharArray()); digitToWord("2".toCharArray()); digitToWord("0".toCharArray()); digitToWord("801".toCharArray()); digitToWord("54312".toCharArray()); //passing empty string digitToWord("".toCharArray()); } static void digitToWord(char n[]) { int len = n.length; if (len == 0) { System.out.println("Empty string."); return; } if (len > 4) { System.out.print(String.valueOf(n) + ": "); System.out.println("The number you have entered has more than four digits."); return; } String[] unitPlaceDig = new String[] {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; String[] tensPlaceDig = new String[] {"", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; String[] tensMultiplier = new String[] {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; String[] powOfTens = new String[] {"Hundred", "Thousand"}; System.out.print(String.valueOf(n) + ": "); if (len == 1){ System.out.println(unitPlaceDig[n[0]-'0']); return; } int p = 0; while (p < n.length) { if (len >= 3){ if (n[p] - '0' != 0) { System.out.print(unitPlaceDig[n[p] - '0'] + " "); System.out.print(powOfTens[len - 3]+ " "); } --len; } else { if (n[p] - '0' == 1) { int sum = n[p] - '0' + n[p + 1] - '0'; System.out.println(tensPlaceDig[sum]); return; } else if (n[p] - '0' == 2 && n[p + 1] - '0' == 0) { System.out.println("Twenty"); return; } else { int i = (n[p] - '0'); if (i > 0) System.out.print(tensMultiplier[i]+ " "); else System.out.print(""); ++p; if (n[p] - '0' != 0) System.out.println(unitPlaceDig[n[p] - '0']); } } ++p; } } }
Output
1234: One Thousand Two Hundred Thirty Four 557: Five Hundred Fifty Seven 45: Forty Five 2: Two 0: Zero 801: Eight Hundred One 54312: The number you have entered has more than four digits. Empty string.
In this article, we explored how to take input of a number and display the number in words in Java.