Results 1 to 3 of 3
- 03-09-2012, 05:59 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Using recursion to print alternating squares?
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!
public static void squares(int n)
{
if (n < 1)
{
throw new IllegalArgumentException();
} else
{
System.out.print(n*n);
squares(n-1);
}
}
- 03-09-2012, 07:29 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Using recursion to print alternating squares?
It's probably easiest just to make one method for the odd squares and one for the even squares. You can also make 2 arrays, one for even, one for odd and at each step just fill the proper array and then make your recursive call.
- 03-09-2012, 09:26 AM #3
Re: Using recursion to print alternating squares?
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
alternating font color
By droidus in forum New To JavaReplies: 2Last Post: 12-14-2011, 04:46 PM -
Alternating between players moves in a game
By nephos in forum New To JavaReplies: 3Last Post: 04-18-2011, 08:39 AM -
alternating series sum java help
By java157 in forum New To JavaReplies: 18Last Post: 03-20-2011, 03:41 AM -
Alternating Sum
By ScaryJello in forum New To JavaReplies: 6Last Post: 10-13-2009, 09:18 AM -
strange alternating array
By jarvis in forum New To JavaReplies: 2Last Post: 04-23-2009, 09:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks