1
2public class Main {
3
4 public static void main(String[] args) {
5 System.out.println(reverse(1000));
6 System.out.println(reverse(-2));
7 System.out.println(getDigitCount(4567));
8 numberToWords(1000);
9 numberToWords(-12);
10 numberToWords(123);
11 // write your code here
12 }
13 public static void numberToWords(int number){
14 String numString=""; // initialising an empty string
15 if(number<0){
16 System.out.println("Invalid Value");
17 }
18 int numDigits=getDigitCount(number); //calling getDigitCount() method
19 int reverseNum=reverse(number); // calling reverse() method
20 int testNum=0;
21 while (numDigits>0){
22 testNum=reverseNum%10;
23 reverseNum/=10;
24 numDigits-=1;
25 if(testNum==0){
26 numString+="Zero ";
27 }
28 else{
29 switch(testNum){
30 case 1:
31 numString += "One ";
32 break;
33 case 2:
34 numString += "Two ";
35 break;
36 case 3:
37 numString += "Three ";
38 break;
39 case 4:
40 numString += "Four ";
41 break;
42 case 5:
43 numString += "Five ";
44 break;
45 case 6:
46 numString += "Six ";
47 break;
48 case 7:
49 numString += "Seven ";
50 break;
51 case 8:
52 numString += "Eight ";
53 break;
54 case 9:
55 numString += "Nine ";
56 break;
57
58 }
59 }
60 }
61 System.out.println(numString);
62
63 }
64
65 public static int reverse(int number){
66 int reverseNum=0;
67
68 while(number!=0){
69 int lastDigit=number%10;
70 reverseNum=(reverseNum*10)+lastDigit;
71 number=number/10;
72 }
73 return reverseNum;
74 }
75
76
77 public static int getDigitCount(int number){
78 int digit=0;
79 if(number<0){
80 return -1;
81 }
82 if(number==0){
83 digit=1;
84 }
85 while(number>0){
86 number=number/10;
87 digit+=1;
88 }
89 return digit;
90 }
91}