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

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

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?

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.

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?

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?

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?

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?

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

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");}

}
}
}```
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"
Last edited by Norm; 04-20-2012 at 02:34 AM. Reason: changed quote to code

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

the program doesn't tell whether all digits are even or not
What does the False that was printed out say about the number?

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.

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

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.

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?

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

how should I change the program
What do you want the program to do differently?

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;}
}
}
}

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.

missing return statement
What if the loop never executes?

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?

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;
}
}```
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 =)

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

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.

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()?

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.

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

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");
}
}```
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;"...
Last edited by B-52 Stratofortress; 04-20-2012 at 03:28 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•