| Author |
From a given string of numeric values, print the individual numbers in words separated by space.
|
Aneek Banerjee
Ranch Hand
Joined: Jun 20, 2012
Posts: 32
|
|
From a given string of numeric values, print the individual numbers in words separated by space.
Input: “2491"
i have written the below prog for the same.
public class notoword {
public static void main(String[] args) {
int n=2346980;
int d;int rev=0;
while(n!=0){
d=n%10;
rev=rev*10+d;
n=n/10;
}
int num=rev;
while(num!=0){
d=num%10;
switch(d){
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;
}
num=num/10;
}
}
}
its not giving output for any string like 2340,9070.
I know I am dividing it by 10 so there is no output for this kind of input.
Please correct my programme as required.And can any one suggest any better solution for this.
Thanks in advance.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16813
|
|
Aneek Banerjee wrote:From a given string of numeric values, print the individual numbers in words separated by space.
Input: “2491"
i have written the below prog for the same.
its not giving output for any string like 2340,9070.
I know I am dividing it by 10 so there is no output for this kind of input.
Please correct my programme as required.And can any one suggest any better solution for this.
First, what about zero? Can't zero appear in a number?
Second, is it really a good idea to reverse the number? What is the number 50 in reverse? What is the number 500 in reverse? What is the number 5000000? in reverse? Can you tell the difference?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: From a given string of numeric values, print the individual numbers in words separated by space.
|
|
|