a loop that extracts each digit of a number

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 43
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

a loop that extracts each digit of a number

 
0
  #1
Sep 7th, 2009
i want to extract

576

to

hundreds = 5
tens = 7
ones = 6

my code does this purpose, but..
  1. h=num/100;
  2. t=(num-(h*100))/10;
  3. o=((num-(h*100))-(t*10));

i want to rewrite my code using loop and modulus operator..

help guys. thanks

btw: i'm writing a program that converts numbr to w.ord-/.
a joke.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: a loop that extracts each digit of a number

 
0
  #2
Sep 7th, 2009
Convert integer number to String. Length of of this String gives you info, how many digits are. Create int[string.length()] array. Use loop to transcribe all values. On fly convert particular values to int.
Find a better solution (using % and / operators).
Last edited by quuba; Sep 7th, 2009 at 3:17 am. Reason: added last line
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,621
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: a loop that extracts each digit of a number

 
0
  #3
Sep 7th, 2009
@scias23 : if I can't get the code from working, pause for a while and ask yourself, "am i coding the right code?"
-- while!
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 43
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

Re: a loop that extracts each digit of a number

 
0
  #4
Sep 7th, 2009
i can't think of a solution that uses the modulo operator

Originally Posted by quuba View Post
Convert integer number to String. Length of of this String gives you info, how many digits are. Create int[string.length()] array. Use loop to transcribe all values. On fly convert particular values to int.
Find a better solution (using % and / operators).
a joke.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: tha_ratl is an unknown quantity at this point 
Solved Threads: 0
tha_ratl tha_ratl is offline Offline
Newbie Poster

Re: a loop that extracts each digit of a number

 
0
  #5
Sep 7th, 2009
Hi!
I suggest you should read on this and you will find the solution easily..
But as a help,
You can use % operator to simplify do it.
  1. int a=563%100; //a=63
  2. int b=a%10; //b=3
use % with / and you can simply do the job...

I hope this helps you..
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: a loop that extracts each digit of a number

 
0
  #6
Sep 7th, 2009
Laboratory:
  1. public class Convert {
  2.  
  3. //1. base form
  4. static int[] convert(int k) {
  5. int[] result = null;
  6. return result;
  7. }
  8.  
  9. static int[] convert2(int k) {
  10. int size = 3;//arbitrary
  11. int[] result = new int[size];
  12. return result;
  13. }
  14.  
  15. static int[] convert3(int k) {
  16. int size = 8;
  17. int[] result = new int[size];
  18. for (int i = 0; i < size; i++) {
  19. result[i] = k % 10;
  20. // what can i do with k?
  21. }
  22.  
  23. return result;
  24. }
  25.  
  26. public static void main(String[] args) {
  27. int k = 576;
  28. int[] result = convert(k);
  29. //int[] result = convert2(k);
  30. //int[] result = convert3(k);
  31. System.out.println(k);
  32. System.out.println(result[0]);
  33. System.out.println(result[1]);
  34. System.out.println(result[2]);
  35. //System.out.println(result[3]);
  36. }
  37. }
array is a good place, to store calculated values.
Write once, use many times.
Continue...
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: tha_ratl is an unknown quantity at this point 
Solved Threads: 0
tha_ratl tha_ratl is offline Offline
Newbie Poster

Re: a loop that extracts each digit of a number

 
0
  #7
Sep 7th, 2009
i'm writing a program that converts numbr to w.ord-/.
If your final purpose is this then I think it is more simple to convert the no to string and then process it.. It is straight forward.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 43
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

Re: a loop that extracts each digit of a number

 
0
  #8
Sep 7th, 2009
i'm writing a program that converts numbers to words.. from 0-9999. it's inefficient to convert the number to string for this purpose.

Originally Posted by tha_ratl View Post
If your final purpose is this then I think it is more simple to convert the no to string and then process it.. It is straight forward.
a joke.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC