programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Recursion

Shane McKenzie
Greenhorn
Posts: 10
Man, does recursion get on my nerves. :-) Here's my problem, and I have literally no idea where to even start, or I'd post a code snippet. I'm just about to take a final exam in a programming class, and I've had no trouble with anything to this point, except recursion. It drives me mad. What I need to do is create a method called remainder(int a, int b), that calculates the remainder (a % b) WITHOUT using the % operator. I pointed out the presence of the static methjod Math.pow() to my professor, and he was nonplussed. :-| Can anyone point me in the right direction???

shane

Darryl Nortje
Ranch Hand
Posts: 140
Hi Shane

You said
--> What I need to do is create a method called remainder(int a, int b),
--> that calculates the remainder (a % b) WITHOUT using the % operator.

Do you have to recursion?? 'cos this method is possible with simple maths.
a = 10
b = 3

a/b = 3
a/b * b = 9
a - (a/b * b) = 1

Or must you use recursion???

cheers
Darryl

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
The long way to do division is subtraction. What if the method remainder(a,b) said if a < b then return a, else do some subtraction and call itself. (Deliberately vague suggestion.)

Of course you can do this more efficiently in a loop, but if the objective is to play with recursion, then recurse (is that a verb?) with a smile.

jin sun
Ranch Hand
Posts: 30
^ Yea I think he's being tested on recursion, therefore he needs to show a recursion example to prove he understands the concept, thus efficiency is irrevelant for this situation.
[ July 25, 2005: Message edited by: jin sun ]