• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using recursion to print alternating squares?

 
Bear Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/56462-using-recursion-print-alternating-squares.html

Any more?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Kim, you wasted my time!
 
Bear Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Kim wrote:why is cross-posting bad?

refer Darryl's Post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic