| Author |
Using recursion to print alternating squares?
|
Bear Kim
Greenhorn
Joined: Feb 25, 2012
Posts: 9
|
|
Hi, I'm trying to use recursion (no loops) to print out descending odd, then ascending even squares less than a certain number.
For examples, squares(5) would print out: 25, 9, 1, 4, 16.
I'm not sure how I would alternate between even/odd, and would really appreciate some help with that.
Right now, I have it so that all squares are printed in descending order. All input is greatly appreciated!
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
You are almost done.
One Suggestion: why do you throw Exception from base condition, I do suggest you to just return.
Hint : even number * even number => even number ; odd number * odd number => odd number.
So, when you encounter even number dont print it instead put into a *X* datastructure. and print it later in base condition before return.
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4167
|
|
Bear, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/56462-using-recursion-print-alternating-squares.html
Any more?
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
Good Kim, you wasted my time!
|
 |
Bear Kim
Greenhorn
Joined: Feb 25, 2012
Posts: 9
|
|
|
Sorry, I'm confused, why is cross-posting bad? Thanks for the help anyways though, other site wasn't much help.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Bear Kim wrote:why is cross-posting bad?
refer Darryl's Post.
|
 |
 |
|
|
subject: Using recursion to print alternating squares?
|
|
|