# Make a recurisive program that determines whether all digits are even or not...

• 04-19-2012, 11:07 PM
B-52 Stratofortress
Make a recurisive program that determines whether all digits are even or not...
I use this method to separate the digits

Quote:

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;
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?
• 04-19-2012, 11:25 PM
Norm
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.
• 04-19-2012, 11:49 PM
B-52 Stratofortress
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
Norm
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?
• 04-20-2012, 12:12 AM
B-52 Stratofortress
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
Norm
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?
• 04-20-2012, 02:30 AM
B-52 Stratofortress
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

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");}         } } }```
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"
• 04-20-2012, 02:34 AM
Norm
Re: Make a recurisive program that determines whether all digits are even or not...
Quote:

the program doesn't tell whether all digits are even or not
What does the False that was printed out say about the number?
• 04-20-2012, 02:35 AM
B-52 Stratofortress
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...
• 04-20-2012, 02:39 AM
Norm
Re: Make a recurisive program that determines whether all digits are even or not...
Quote:

false suppose to mean all digits are even
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.
• 04-20-2012, 02:40 AM
B-52 Stratofortress
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
Norm
Re: Make a recurisive program that determines whether all digits are even or not...
Quote:

how should I change the program
What do you want the program to do differently?
• 04-20-2012, 02:50 AM
B-52 Stratofortress
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...

Quote:

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
Norm
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.

Quote:

missing return statement
What if the loop never executes?
• 04-20-2012, 03:15 AM
B-52 Stratofortress
Re: Make a recurisive program that determines whether all digits are even or not...
I think I got it, is this correct?

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; } }```
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 =)
• 04-20-2012, 03:18 AM
Norm
Re: Make a recurisive program that determines whether all digits are even or not...
Quote:

is this correct?
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.

One thing you might add is a print out of the results.
• 04-20-2012, 03:20 AM
B-52 Stratofortress
Re: Make a recurisive program that determines whether all digits are even or not...
System.out.print()?
• 04-20-2012, 03:23 AM
Norm
Re: Make a recurisive program that determines whether all digits are even or not...
However you want to report the results to the user.
• 04-20-2012, 03:26 AM
B-52 Stratofortress
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

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"); } }```
How do you make the program to go to an end after the printing out "False", because it prints out "FalseTrue" on mine.

Edit: Wait, nevermind, I used "break;" instead of "return;"...