This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

From a given string of numeric values, print the individual numbers in words separated by space.  RSS feed

 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!