This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Using recursion to print alternating squares? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using recursion to print alternating squares?" Watch "Using recursion to print alternating squares?" New topic
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: 4523
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using recursion to print alternating squares?
 
Similar Threads
Help with Arrays!
Power function
while loop
Discussion on recursion, stack overflows, & compiler optimization for tail recurssion
Timing Methods