d named hasAnOddDigit that returns whether any digit of a positive integer is odd. Your method should return true if the number has at least one odd digit and false if none of its digits are odd. 0, 2, 4, 6, and 8 are even digits, and 1, 3, 5, 7, 9 are odd digits.
For example, here are some calls to your method and their expected results:
Call | Value Returned |
hasAnOddDigit(4822116) |
true |
hasAnOddDigit(2448) |
false |
hasAnOddDigit(-7004) |
true |
You should not use a String to solve this problem.
my code:
public boolean hasAnOddDigit(int num){
// You do not need to make a copy of num, because
// of pass-by-value semantic: since num is a copy,
// you can change it inside the method as you see fit.
while (num > 0) {
int digit = num % 10;
if (digit % 2 == 1) return true;
num /= 10;
}
// If we made it through the loop without hitting an odd digit,
// all digits must be even
return false;
}
error:
# | name | expected return | your return | result |
1 |
hasAnOddDigit(4822116) |
true |
true |
pass |
2 |
hasAnOddDigit(2448) |
false |
false |
pass |
3 |
hasAnOddDigit(8910437) |
true |
true |
pass |
4 |
hasAnOddDigit(6022048) |
false |
false |
pass |
5 |
hasAnOddDigit(824876248) |
true |
true |
pass |
6 |
hasAnOddDigit(1375335) |
true |
true |
pass |
7 |
hasAnOddDigit(42) |
false |
false |
pass |
8 |
hasAnOddDigit(70) |
true |
true |
pass |
9 |
hasAnOddDigit(4) |
false |
false |
pass |
10 |
hasAnOddDigit(3) |
true |
true |
pass |
11 |
hasAnOddDigit(0) |
false |
false |
pass |
12 |
hasAnOddDigit(-7004) |
true |
false |
fail |
13 |
hasAnOddDigit(-24806) |
false |
false |
pass |