Results 1 to 3 of 3
  1. #1
    hydride is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default Using method to call Factorial

    public static long factorial(int max) {

    long factorial = 0;
    for (int i = 2; i <= max; i++) {
    if( i <= 1 ) // base case
    return 1;
    else

    return i * factorial( i - 1 );
    System.out.println("Factorial " + i + " value " + i);
    }

    return factorial;
    }
    Help meee:( im trying to print out the factorial for max. I know how the factorial process works, but i dont get how this works.
    thanks a whole bunch.

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    That program won't work. You are trying to combine recursive and iterative methods of finding the factorial. To do recursive:
    Java Code:
    public static int factorial(int a)
    {
    if(a == 1)
    return 1;
    return a*Classname.factorial(a-1);
    }
    for iterative:
    Java Code:
    public static int factorial(int a)
    {
    int n;
    for(int i=a; i>1; i--)
    {
    n=n*i;
    }
    return n;
    }

  3. #3
    hydride is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

Similar Threads

  1. How to call the equals method
    By random0munky in forum New To Java
    Replies: 1
    Last Post: 10-12-2009, 05:33 PM
  2. How to call a method from another class?
    By jboy in forum New To Java
    Replies: 8
    Last Post: 09-09-2009, 07:29 AM
  3. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-29-2009, 11:55 PM
  4. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM
  5. Call a Method Automatically
    By rhm54 in forum New To Java
    Replies: 4
    Last Post: 02-07-2008, 08:51 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
  •