odd and even digits

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

Join Date: Oct 2009
Posts: 5
Reputation: ki72 is an unknown quantity at this point 
Solved Threads: 0
ki72 ki72 is offline Offline
Newbie Poster

odd and even digits

 
0
  #1
Oct 22nd, 2009
Java Code
I don't know how to show odd or even digits from an integer number. For example I ask the user to enter a 4 digits integer number. For example the user enter 4875. I want to display odd digits (7 5) and even digits (4 8). By Extract each digit of the number using integer division and store the results in separate variables.
for example, that 4875 is 4*1000+8*100+7*10+5
Last edited by ki72; Oct 22nd, 2009 at 3:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,197
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
-1
  #2
Oct 22nd, 2009
Be creative, for example if you read input from user you will get string object. Convert string to character array, read array element by element, convert element to integer and fine out if it is odd or even number.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,597
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #3
Oct 22nd, 2009
In addition to what Peter said, you might find some of the responses here helpful: http://www.daniweb.com/forums/thread232056.html
Out.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training
 
-1
  #4
Oct 22nd, 2009
  1. int a=0,b=0,c=0,d=0;
  2. int n=4875;
  3. int rest=0,i=1;
  4.  
  5.  
  6. while(n!=0)
  7. {
  8. rest = n%10;//ghet the last digit of the number
  9. n=(n-rest)/10; //the number gets the last digit out
  10.  
  11.  
  12. switch(i)
  13. {
  14. case 1:a=rest;break;
  15. case 2:b=rest;break;
  16. case 3:c=rest;break;
  17. case 4:d=rest;break;
  18. }
  19. i++;
  20. }
  21. //see what we got
  22. System.out.println(Integer.toString(a));
  23. System.out.println(Integer.toString(b));
  24. System.out.println(Integer.toString(c));
  25. System.out.println(Integer.toString(d));
  26. //after you have the variables you can test if(variable%2==0) is odd else its even
Good luck, reminds me of my first year of Computer Science (i was coding this problem in C)
Last edited by Clawsy; Oct 22nd, 2009 at 3:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training
 
0
  #5
Oct 23rd, 2009
Can someone please tell me why I have been down-voted for previous post? I really wanted to solution this problem. I am used to explaint a lot of students facts like this so that's why I gave ki72 the answer! If I would have bad manners fo course I should have been down-voted.
I really don't understand. Someone please tell me. And maybe don't down-vote this post, too. Really. No disrespect intended.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,197
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
-1
  #6
Oct 23rd, 2009
  • Guess it will be down to the fact we have here nice rule - We only give homework help to those who show effort that you ignored.
  • Next OP already had two hints how to approach this problem, therefore your solution wasn't necessary.
  • Lastly our suggestions been better in comparison yours where if user provide number in different format then 4 digits program will not work correctly
Do you still believe that person who down-voted you made wrong decision?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC