recursion problem, please help

Printable View

• October 18th, 2010, 08:58 AM
nil
recursion problem, please help
I need to write a code that recieve an integer and find whether it forms an ascending series or not.
for example:
123 - true
2579 - true
321 - false
1338 - false
222 - false
5 - true

I wrote this recursion but i don't get the right result
can someone tell me what's wrong with it please?
Thanks...

Code Java:

```private static boolean numCheck(int num) {   if ((num/10) == 0) { return true; } else { if ((num % 10) > ((num/10) % 10)) { numCheck(num/10); } else { return false; } }   }```
• October 18th, 2010, 09:04 AM
KevinWorkman
Re: recursion problem, please help
What result do you get instead? What should the algorithm do?

Post an SSCCE that demonstrates the problem, and we'll go from there.
• October 18th, 2010, 12:59 PM
javapenguin
Re: recursion problem, please help
Quote:

Originally Posted by nil
I need to write a code that recieve an integer and find whether it forms an ascending series or not.
for example:
123 - true
2579 - true
321 - false
1338 - false
222 - false
5 - true

I wrote this recursion but i don't get the right result
can someone tell me what's wrong with it please?
Thanks...

private static boolean numCheck(int num) {

if ((num/10) == 0) {
return true;
} else {
if ((num % 10) > ((num/10) % 10)) {
numCheck(num/10);
} else {
return false;
}
}

}

I can't see anything wrong, unless you're entering negative values.

123%10 = 3

123/10 = 12;

12%10 = 2;

3 > 2;

numCheck(12);

12%10 = 2;

12/10 = 1;

1%10 = 1;

2 > 1;

numCheck(1);

1/10 = 0;

return true;

122%10 = 2;

122/10 = 12;

12%10 = 2;

2 = 2;

return false;