| | |
odd and even digits
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
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
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.
-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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Sep 2008
Posts: 1,597
Reputation:
Solved Threads: 202
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.
•
•
Join Date: Feb 2008
Posts: 99
Reputation:
Solved Threads: 5
-1
#4 Oct 22nd, 2009
Java Syntax (Toggle Plain Text)
int a=0,b=0,c=0,d=0; int n=4875; int rest=0,i=1; while(n!=0) { rest = n%10;//ghet the last digit of the number n=(n-rest)/10; //the number gets the last digit out switch(i) { case 1:a=rest;break; case 2:b=rest;break; case 3:c=rest;break; case 4:d=rest;break; } i++; } //see what we got System.out.println(Integer.toString(a)); System.out.println(Integer.toString(b)); System.out.println(Integer.toString(c)); System.out.println(Integer.toString(d)); //after you have the variables you can test if(variable%2==0) is odd else its even
Last edited by Clawsy; Oct 22nd, 2009 at 3:45 pm.
•
•
Join Date: Feb 2008
Posts: 99
Reputation:
Solved Threads: 5
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.
I really don't understand. Someone please tell me. And maybe don't down-vote this post, too. Really. No disrespect intended.
-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
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Looping Problem and some questions (C++)
- making counter for odd numbers (C++)
- help writing a string to textfile with fprintf (C)
- Help with program to convert hex to dec (Assembly)
- write to text file using fprintf() (C)
- problems with function pointers (C)
- can't delete single chars in a string (C)
- cin.get problems in use (C)
- C++:three-digit number all odd, eve, or both (C++)
Other Threads in the Java Forum
- Previous Thread: web downloader
- Next Thread: how to add adt(android) plugin to eclipse from zip file
| Thread Tools | Search this Thread |
2dgraphics android api apple applet application arguments array arrays automation banking binary binarytree bluetooth capture chat chatprogramusingobjects class classes client code color component count database derby design eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list loop macintosh map method methods midlethttpconnection mobile netbeans newbie object os print printing problem producer program programming project projectideas read recursion reference replaysolutions ria scanner screen server set size sms sort sourcelabs sql stop string swing threads transforms tree ui unicode validation windows






