Results 1 to 19 of 19
- 04-19-2012, 11:07 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Make a recurisive program that determines whether all digits are even or not...
I use this method to separate the digits
There is a problem, I can only the separate up to 7 digits, if the number has more than 7 digits, my program won't work, so is there a better way to do so?a = x / 1000000;
b = (x - (a * 1000000)) / 100000;
c = (x - (a * 1000000) - (b * 100000)) / 10000;
d = (x - (a * 1000000) - (b * 100000) -(c * 10000)) / 1000;
e = (x - (a * 1000000) - (b * 100000) -(c * 10000) - (d * 1000)) / 100;
f = (x - (a * 1000000) - (b * 100000) -(c * 10000) - (d * 1000) - (e * 100)) / 10;
g = (x - (a * 1000000) - (b * 100000) -(c * 10000) - (d * 1000) - (e * 100) - (f * 10)) / 1;
- 04-19-2012, 11:25 PM #2
Re: Make a recurisive program that determines whether all digits are even or not...
Start at the other end and divide by 10. Use % to get the right most digit.
If you don't understand my response, don't ignore it, ask a question.
- 04-19-2012, 11:49 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Re: Make a recurisive program that determines whether all digits are even or not...
Should I use while loop or for loop?
Edit: I am thinking for loop, how should I write it?
- 04-19-2012, 11:51 PM #4
Re: Make a recurisive program that determines whether all digits are even or not...
The type of loop is determined by how you want to end it and if you want index variables.
When do you want the loop to end?If you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 12:12 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Re: Make a recurisive program that determines whether all digits are even or not...
The loop should end when goes down to the whole number digit?
- 04-20-2012, 01:32 AM #6
Re: Make a recurisive program that determines whether all digits are even or not...
Can you write a condition that you could use in a while statement to test for that?
If you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 02:30 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Re: Make a recurisive program that determines whether all digits are even or not...
I am suppose to write a Recursive with boolean, but I wrote
I did successfully separate the digits, but the program doesn't tell whether all digits are even or not, I typed in "2478", it printed out "TrueFalseTrueTrue"Java Code:import javax.swing.JOptionPane; public class Recursive_13 { public static void main () { String number; int x; int n; x = Integer.parseInt(JOptionPane.showInputDialog ( "Enter the a #: " )); while (x > 0) { n = x%10; x = x/10; if (n%2==0) { System.out.print("True");} else { System.out.print("False");} } } }Last edited by Norm; 04-20-2012 at 02:34 AM. Reason: changed quote to code
- 04-20-2012, 02:34 AM #8
Re: Make a recurisive program that determines whether all digits are even or not...
What does the False that was printed out say about the number?the program doesn't tell whether all digits are even or notIf you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 02:35 AM #9
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Re: Make a recurisive program that determines whether all digits are even or not...
The "false" suppose to mean all digits are odd, but in my program (which is wrong), printed out whether the individual digit is odd or not. Not sure how to fix this...
Last edited by B-52 Stratofortress; 04-20-2012 at 02:39 AM.
- 04-20-2012, 02:39 AM #10
Re: Make a recurisive program that determines whether all digits are even or not...
I not sure I understand what you are saying. That is not how code is written. If an even digit is to print false, then swap the "true" and "false" messages that the code prints.false suppose to mean all digits are evenIf you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 02:40 AM #11
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Re: Make a recurisive program that determines whether all digits are even or not...
Sorry, that's a typo, even should be "True", and odd should be "False", but how should I change the program though?
- 04-20-2012, 02:46 AM #12
Re: Make a recurisive program that determines whether all digits are even or not...
What do you want the program to do differently?how should I change the programIf you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 02:50 AM #13
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Re: Make a recurisive program that determines whether all digits are even or not...
I wish to make a program to determine whether if all digits are even or not. For example, if it's 2478, it should print out "False", instead of "TrueFalseTrueTrue", it should only print out true if all digits are even.
Edit: I got another program, it says I am missing return statement...
import javax.swing.JOptionPane;
public class Recursive_13
{
public void init () {
String number;
int a;
a = Integer.parseInt(JOptionPane.showInputDialog ( "Enter the a #: " ));
areDigitsAllEvenRecursively(a);
}
public boolean areDigitsAllEvenRecursively(int x) {
int n;
while (x > 0) {
n = x%10;
x = x/10;
if (n%2==0) {
return true;}
else {
return false;}
}
}
}
- 04-20-2012, 02:58 AM #14
Re: Make a recurisive program that determines whether all digits are even or not...
If you don't want to see True printed, comment out the line that prints it.
Have the program set a boolean variable when it prints False.
At the end of the loop if the boolean variable is not set, print True.
Your areDigitsAllEvenRecursively() method returns true or false on the first digit it looks at. What about the rest of the digits?
false is OK, but true should wait until all digits have been looked at.
What if the loop never executes?missing return statementIf you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 03:15 AM #15
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Re: Make a recurisive program that determines whether all digits are even or not...
I think I got it, is this correct?
The trick is to check out all the "false" first then the "true", because if one digit is odd, then the entire statement is false. Thanks for helping =)Java Code:import javax.swing.JOptionPane; public class Recursive_13 { public void init () { String number; int a; a = Integer.parseInt(JOptionPane.showInputDialog ( "Enter the a #: " )); areDigitsAllEvenRecursively(a); } public boolean areDigitsAllEvenRecursively(int x) { int n; while (x > 0) { n = x%10; x = x/10; if (n%2!=0) { return false;} } return true; } }
- 04-20-2012, 03:18 AM #16
Re: Make a recurisive program that determines whether all digits are even or not...
A good way to find out is to test the code with different input and see if it does what you want. That's what I'd have to do.is this correct?
One thing you might add is a print out of the results.If you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 03:20 AM #17
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Re: Make a recurisive program that determines whether all digits are even or not...
System.out.print()?
- 04-20-2012, 03:23 AM #18
Re: Make a recurisive program that determines whether all digits are even or not...
However you want to report the results to the user.
If you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 03:26 AM #19
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Re: Make a recurisive program that determines whether all digits are even or not...
I got it on my Recurisve one, now I have work on the regular one
How do you make the program to go to an end after the printing out "False", because it prints out "FalseTrue" on mine.Java Code:import javax.swing.JOptionPane; public class Original_13 { public static void main () { String number; int x; int n; x = Integer.parseInt(JOptionPane.showInputDialog ( "Enter the a #: " )); while (x > 0) { n = x%10; x = x/10; if (n%2!=0) { System.out.print("False"); break;} } System.out.print("True"); } }
Edit: Wait, nevermind, I used "break;" instead of "return;"...Last edited by B-52 Stratofortress; 04-20-2012 at 03:28 AM.
Similar Threads
-
Java help- 25-element array of digits to store integers as large as 25 digits
By CCC3 in forum New To JavaReplies: 3Last Post: 10-27-2011, 05:23 PM -
A program where a user enters digits which returns words using arrays
By Dev23 in forum New To JavaReplies: 0Last Post: 03-17-2011, 12:22 PM -
Java program: ISBN calculated by other 9 digits?
By SNFA in forum New To JavaReplies: 5Last Post: 10-09-2010, 05:22 AM -
can anyone tell my why this program bugs out when i enter more then 10 digits?
By sweetjava in forum New To JavaReplies: 3Last Post: 08-19-2009, 03:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks