Results 1 to 3 of 3
  1. #1
    fmmoss is offline Member
    Join Date
    Jan 2017
    Posts
    1
    Rep Power
    0

    Default Where´s the logic?

    Good day, everyone. I am learning Java and one lesson I was working on is about calculating a factorial using recursion. The following is the code for it.

    public class Application {
    public static void main(String[] args) {
    int value;
    // e.g. 4! = 4*3*2*1 (factorial 4)
    System.out.println(factorial(4));
    }

    private static int factorial(int value) {

    if(value == 1) {
    return 1;
    }

    return factorial(value-1) * value;
    }
    }

    My question is where in the code is 4 being multiplied by 3 and then by 2 etc? I think I understand how 4 is getting mulitplied by 3, but then I can´t follow the rest of the logic. Can someone please explain it to me?

    Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,624
    Rep Power
    29

    Default Re: Where´s the logic?

    where in the code is 4 being multiplied by 3 and then by 2 etc?
    Where is the * operator used in the code? That will be where the values are multiplied.
    Add some print statements to print out the values used with the * operator.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: Where´s the logic?

    You should use pencil and paper to record the values and go thru it to see how it works. Here are some simpler examples to start with. The important thing to remember is that values are saved on the stack as each recursive call is made. Also note the only difference of the two methods is the location of the print statement.

    Java Code:
    public class RecursionDemo {
       public static void main(String[] args) {
          printAscending(6);
          System.out.println("---------");
          printDescending(6);
          
       }
       
       // print values from 1 thru n inclusive
       public static void printAscending(int n) {
          if (n > 1) {
             printAscending(n-1);
          }
          System.out.println(n);
       }
       
       // print values from n thru 1 inclusive
       public static void printDescending(int n) {
          System.out.println(n);
          if (n > 1) {
             printDescending(n-1);
          }
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Where's the logic?
    By diamonddragon in forum New To Java
    Replies: 11
    Last Post: 01-28-2012, 05:16 AM
  2. Logic Help
    By Barbados in forum New To Java
    Replies: 6
    Last Post: 01-23-2012, 12:24 AM
  3. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 07:44 PM
  4. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 12:06 PM
  5. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 02:18 PM

Posting Permissions

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