-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumbToWord.java
More file actions
74 lines (68 loc) · 1.32 KB
/
NumbToWord.java
File metadata and controls
74 lines (68 loc) · 1.32 KB
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
70
71
72
73
74
// Convert Number to words
import java.util.*;
public class NumbToWord
{
public static void main(String args[])
{
int n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number: ");
n=sc.nextInt();
try{
if(n==0)
System.out.println("Zero");
else
System.out.println(" Number To Words: "+nToW(n));
}
catch(Exception e)
{
System.out.println("Please Enter a Valid Number!");
}
}
public static String nToW(int n)
{
String unit[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine",
"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
String ten[]={"Zero","Ten","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
String words=" ";
if(n==0)
return "Zero";
if(n<0)
{
String negative=""+n;
negative=negative.substring(1);
return "minus"+nToW(Integer.parseInt(negative));
}
if((n/1000000)>0)
{
words=nToW(n/1000000)+" Million ";
n %=1000000;
}
if((n/1000)>0)
{
words=nToW(n/1000)+" Thousand ";
n %=1000;
}
if((n/100)>0)
{
words=nToW(n/100)+" Hundred ";
n %=100;
}
if(n>0)
{
if(n<20)
{
words+=unit[n];
}
else
{
words+=ten[n/10];
if(n%10>0)
{
words+="_"+unit[n%10];
}
}
}
return words;
}
}