In this tutorial you will learn about the Java Program to convert Number to Word and its application with practical example.
In this tutorial, we will learn to create a Java Program to convert Number to Word.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following java programming topics:
- Java Operators.
- Basic Input and Output
- Class and Object.
- Basic Java programming.
- String.
- User define Function.
Java Program to convert Number to Word
In this program we will convert Number to Word. We would first declared and initialized the required variables. Next, we would ask user to enter a number. let have a look at Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
// java program to covert number to string... import java.util.Scanner; import java.util.Scanner; public class Number_to_word { public static void main(String[] args) { int num = 0; Scanner scanner = new Scanner(System.in); System.out.println("Please enter a number(max upto 9 digits)"); try { num = scanner.nextInt(); if (num == 0) { System.out.print("Number in words: Zero"); } else { System.out.print("Number in words: " + number_Word(num)); } } catch (Exception e) { System.out.println("Please enter a valid number"); } // close the reader scanner.close(); } private static String number_Word(int num) { String words = ""; String unitarr[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; String tensarr[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; if (num == 0) { return "zero"; } if (num < 0) { // converting the number to a string String numberStr = "" + num; // removing minus before the number numberStr = numberStr.substring(1); // add minus before the number and convert the rest of number return "minus " + number_Word(Integer.parseInt(numberStr)); } // cconditon for divisible by 1 million if ((num / 1000000) > 0) { words += number_Word(num / 1000000) + " million "; num %= 1000000; } // cconditon for divisible by 1 thousand if ((num / 1000) > 0) { words += number_Word(num / 1000) + " thousand "; num %= 1000; } // cconditon for divisible by 1 hundred if ((num / 100) > 0) { words += number_Word(num / 100) + " hundred "; num %= 100; } if (num > 0) { if (num < 20) { words += unitarr[num]; } else { words += tensarr[num / 10]; if ((num % 10) > 0) { words += "-" + unitarr[num % 10]; } } } return words; } } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- num = it will hold entered Number
- word= it will hold word values.
- unitarr[]= it will hold unit value of word.
- tensarr[]=it will hold tense value of word.
After that we declare variables and taking value from user.
the following code supports numbers up-to 9999 digits, i.e., numbers from 0 to 9999.
here we create arrays that break individual parts of a strings. One array is used for single digits (unitarr[]), one for numbers from 10 to 19,. etc (tensarr[]). and one for powers of 10. and multiple condition that number is >1000 ,>100 ans so on..as shoen in image below.
And finally we will print the characters or given numbers.