Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: my first recursion ,what did i do wrong?

1. my first recursion ,what did i do wrong?

public static boolean q8(int n){
boolean check=true;

if((n/10)<0){
return check;
}
int f=n%10;
int r=(n/10)%10;
if((f%2==0)&&(r%2==0)){
return check=false;
}
q8(n/10);
return check;

}

the recursion need to get a number and return true if each pair of digits are one even and the other odd.
false if not.
how can i write it better ?for example:for the number 230 -->return true(which is correct)
but for the number 243---->return true(which should be false) cause it takes 2 and 0 from the begining.
how can i rewrite correct?

--- Update ---

public static boolean q8(int n){

int f=n%10;
int r=(n/10)%10;
if((f%2==0)&&(r%2==0)&&((n/10)>0)){
return false;
}

return q8(n/10);

}
this is my second try.it works only if the numbers is false.on true numbers it doesnt work?
any idea how to correct me?  Reply With Quote

3. Re: my first recursion ,what did i do wrong?

Have you tried debugging the code to see what it is doing so you can determine what needs to be changed to make it work the way you want? I use println() statements that print out the values of all the variables as they are given values and also the value of args passed to a method.  Reply With Quote

4. Re: my first recursion ,what did i do wrong?

the recursion need to get a number and return true if each pair of digits are one even and the other odd.
false if not.
Can you explain this better?

Show what you mean by "each pair of digits." For example, using your 230, I would think the pairs of digits would be 23, 30, and maybe 20. For 243, 24, 43, and maybe 23. Use an example and show which pairs of digits the program should consider. Then, considering the pairs of digits, show what the method should return, true or false, and explain why.  Reply With Quote