forked from kalaskarpranav/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberToWords.java
102 lines (90 loc) · 2.96 KB
/
NumberToWords.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
public class NumberToWords {
public static void main(String[] args) {
// TODO Auto-generated method stub
numberToWords(100);
}
public static int getDigitCount(int number) {
int count = 0;
if (number <0) {
return -1;
}
if (number == 0) {
count = 1;
}
while (number > 0) {
number = number / 10;
count=count+1;
}
return count;
}
public static int reverse(int number) {
int c = 0;
int d = 0;
while (number != 0) {
c = number % 10;
d = (d * 10) + c;
//System.out.println("reverse of "+number+" is"+d);
number = number / 10;
}
return d;
}
public static void numberToWords(int number) {
int a = 0;
int b = 0;
int c = 0;
int d = 0,e=0;
if (number >= 0) {
c = reverse(number);
e=c;
// System.out.println("reverse of "+number+" is="+c);
d = getDigitCount(number);
if(number==0) {
System.out.println("Zero");
}
while (c > 0) {
a = c % 10;
c = c / 10;
switch (a) {
case 0:
System.out.println("Zero");
break;
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
case 4:
System.out.println("Four");
break;
case 5:
System.out.println("Five");
break;
case 6:
System.out.println("Six");
break;
case 7:
System.out.println("Seven");
break;
case 8:
System.out.println("Eight");
break;
case 9:
System.out.println("Nine");
break;
}
} int reverseCount= getDigitCount(e);
if(reverseCount!=d){
//System.out.println("c is="+c+" d is="+d);
for(int i=0;i!=d-reverseCount;i++){
System.out.println("Zero");} }
}
else
{
System.out.println("Invalid Value");
}
}
}