mystery2
method
public static int countOddDigitsAlt(int n)
{
if(n == 0)
return 0;
int oddDigits = countOddDigitsAlt(n / 10);
if(n % 2 == 1)
oddDigits++;
return oddDigits;
}
The method counts and returns the number of odd digits in its parameter n
. This is just mystery1
/countOddDigits
with a variable used to hold the result of the recursive call. Some students prefer the 2 recursive calls featured in mystery1
. Some students prefer storing the result of the recursive call in a variable. Both do the same thing.