Skip to content

Commit 18dfb91

Browse files
committed
feat: Display digits of a number in words
🧠 Logic: - Accept integer input `n` and reverse it while storing digits in a string `str`. - Reverse is necessary to retain original digit order for word conversion. - Iterate through `str` in reverse (to get original order). - Use a `switch` statement to map each character (digit) to its corresponding word and print it. Signed-off-by: Somesh diwan <[email protected]>
1 parent a88e59b commit 18dfb91

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Section8LoopAB/src/DisplayNumberInWords.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ public static void main(String[] args) {
1111
int r;
1212
String str = "";
1313

14-
while(n>0)
15-
{
14+
while(n>0) {
1615
r=n%10;
1716
n=n/10;
1817
str=str+r;
@@ -23,9 +22,11 @@ public static void main(String[] args) {
2322

2423
char c;
2524

26-
for(int i =str.length()-1;i>=0;i--){
25+
for(int i =str.length()-1;i>=0;i--)
26+
{
2727
c=str.charAt(i);
28-
switch (c) {
28+
switch (c)
29+
{
2930
case '0':System.out.print("Zero ");
3031
break;
3132
case '1':System.out.print("One ");
@@ -49,4 +50,4 @@ public static void main(String[] args) {
4950
}
5051
}
5152
}
52-
}
53+
}

0 commit comments

Comments
 (0)