Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Using recursion to print alternating squares?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. alternating font color
    By droidus in forum New To Java
    Replies: 2
    Last Post: 12-14-2011, 04:46 PM
  2. Alternating between players moves in a game
    By nephos in forum New To Java
    Replies: 3
    Last Post: 04-18-2011, 08:39 AM
  3. alternating series sum java help
    By java157 in forum New To Java
    Replies: 18
    Last Post: 03-20-2011, 03:41 AM
  4. Alternating Sum
    By ScaryJello in forum New To Java
    Replies: 6
    Last Post: 10-13-2009, 09:18 AM
  5. strange alternating array
    By jarvis in forum New To Java
    Replies: 2
    Last Post: 04-23-2009, 09:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •