944,150 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1819
  • Java RSS
Oct 22nd, 2009
0

odd and even digits

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ki72 is offline Offline
5 posts
since Oct 2009
Oct 22nd, 2009
-1
Re: odd and even digits
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.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Oct 22nd, 2009
0
Re: odd and even digits
In addition to what Peter said, you might find some of the responses here helpful: http://www.daniweb.com/forums/thread232056.html
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 22nd, 2009
-1
Re: odd and even digits
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008
Oct 23rd, 2009
0
Re: odd and even digits
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.
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008
Oct 23rd, 2009
-1
Re: odd and even digits
  • 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?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: web downloader
Next Thread in Java Forum Timeline: Java error - Illegal start of expression





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC