Recursion Problem Need Help ASAP

I need help making a recursive program that computes the number of odd digits in a number. This is as far as I have gotten.

Code :

public class ComputeOddNumbers {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Enter an integer:");
Scanner input=new Scanner(System.in);
int number= input.nextInt();
computeoddnumbers(number);
System.out.print("There are "+odds+" odd numbers");
}
private static int odds;
public static void computeoddnumbers(int number)
{
if(number<10)
{
increaseoddscount(number);
}
while(number>10)
{
increaseoddscount(number%10);
}
}
public static int increaseoddscount(int number)
{
if(number==1||number==3||number==5||number==7|number==9)
{
odds++;
}
return odds;
}
}

Re: Recursion Problem Need Help ASAP

Quote:

computes the number of odd digits in a number

I assume you mean how many odd digits in a String of digits. For example "1234" would have 2.

Or does your number have to be an int?

Do you have an algorithm or design for this problem? Once you get that, then we can help you when you have problems writing the code.

Re: Recursion Problem Need Help ASAP

Is there a particular reason this needs to be recursive (as written you do not user recursion)? A few pointers on the code:

1) Using the modulus operator is very handy to determine odds/evens

2) Your while loops seems to be infinite when number > 10 ...in loops such as this you must somehow reset the variable you are evaluating for the while.

3) If you must use recursion, you need a function which calls itself. Your current code does not seem to have this trait.

Re: Recursion Problem Need Help ASAP

Hey thx.. I changed the code a bit since this post... I have decided to use the modulus operator and I still need a bit of help. And yes this problem does require recursion. Heres the problem from the book. @Norm You are exactly right. If the digit is 1234 then it would output there are 2 odd numbers. This is my first time working with recursion

Re: Recursion Problem Need Help ASAP

Heres the modified code

Code :

System.out.println("Enter an integer:");
Scanner input=new Scanner(System.in);
int number= input.nextInt();
computeoddnumbers(number);
System.out.print("There are "+odds+" odd numbers");
}
private static int odds;
public static void computeoddnumbers(int number)
{
if(number<10)
{
increaseoddscount(number);
}
else
{
{
int numtemp =number%10;
increaseoddscount(numtemp);
}
}
}
public static int increaseoddscount(int number)
{
if(number%2==1)
{
odds++;
}
return odds;
}
}

Re: Recursion Problem Need Help ASAP

Here's a nice little piece of pseudo-code that you can follow to create your code:

Code Pseudo:

int countOdds(int number)
If your number is less than 10:
if it's odd, return 1
else return 0
else:
if number is odd, return 1 + countOdds(number / 10)
else return countOdds(number / 10)

Re: Recursion Problem Need Help ASAP

...I just knew it was something simple like that....I feel dumb lol, But thanks man preciate it.