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;