Hello. I need to write a static recursive method that returns the frequency of occurrence a particular digit d in an integer n. For example, if passed (1342457,4) it should return 2, whereas when passed (1342457,6) it should return 0.

I won't put my code in as it is arbitrary, I just need help with the logic in this one; I can't grasp what needs to be done.

I should also note that no variables are allowed besides the two that the user inputs as the integer, and digit to be checked for (so it must be calculated completely recursively).

I should also note that no variables are allowed besides the two that the user inputs as the integer, and digit to be checked for

I think this requires the use of at least one more variable. I would read the integer in as a string, create an array of all it's char's and then compare each one to the digit.

I don't know how to delete this post, ha. But I'm writing over it here to make this thread less confusing..

I think this requires the use of at least one more variable. I would read the integer in as a string, create an array of all it's char's and then compare each one to the digit.

Yeah, that's what I would do in a real world situation as well, also, I agree in the thinking that there needs to be at least one more variable.. Problem is, I can very distinctly remember my professor saying that no more variables are allowed.

Here's an example to get your mind jogging just in case.

// Returns the largest element in an integer array
public static int maxInArray(int[] a, int size)
{
if (size==1)
return (a[0]);
else
{
int temp = maxInArray(a, size-1);

if(temp>a[size-1])
return temp;
else
return a[size-1];
}
}

Also note that he did use a variable in this one, so I guess it's a bad example. I would love to argue with someone about the need of another variable to compute this but I simply don't have the experience with recursion to make a statement (this is my first assignment on recursion, just started the chapter last week).

Here's a hint for you.

If you cannot separate the entered number into characters, the only way you can test individual digits is to divide the number down. If you use modular division (%10) the result will be the final digit in the number which can be tested. Using integer division by 10 would discard the final digit and leave the rest for further testing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.