Welcome to Dream.In.Code
Become a Java Expert!

Join 150,355 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,943 people online right now. Registration is fast and FREE... Join Now!




Factorial recursion

 
Reply to this topicStart new topic

Factorial recursion

naash
19 Feb, 2008 - 05:03 AM
Post #1

D.I.C Head
**

Joined: 25 Aug, 2007
Posts: 77


My Contributions
Calculate factorial using recursion

But upon compilation it gives stack overflow
Help !

CODE

class Factorial
{      
        int n;
        Factorial()
            {
                n=0;
            }      
    int fact(int N)
        {
                n=N;
        int result;
                if(n>1)
          {
                    result=fact(n-1)*n;
            return result;
                  }
                else
                    return result;
        }
}

class Recursion28
{
  public static void main(String args[])
         {
        Factorial f=new Factorial();
        System.out.println("3 ! = " +f.fact(3));
     }
}

User is offlineProfile CardPM
+Quote Post

AbuJaFaR
RE: Factorial Recursion
19 Feb, 2008 - 06:51 AM
Post #2

D.I.C Head
**

Joined: 13 Dec, 2007
Posts: 123


My Contributions
Hello there,
I copy pasted ur code but I didnt get that error. blink.gif
I got that variable result may not be initialised.Anyway,I made some changes.Ignore the comments it was for helping me see what values n is taking.It helps biggrin.gif You should try it sometimes as well.
Check it out and tell me.

CODE

class Factorial
{      
      
       int result;
      
      
       /*Factorial()
       {
          n=0;
          
       } */    
    
    int fact(int N)
    {
        int n=N;
        
        if(n>1)
        {
            result=fact(n-1)*n;
            //System.out.println("RESULT:  "+result);
            return result;
        }
        else
            //System.out.println("N "+n);
            return n;
    }
}

class Recursion28
{
  public static void main(String args[])
  {
        
        Factorial f=new Factorial();
        System.out.println("3 ! = " +f.fact(3));
         
  }
}


This post has been edited by AbuJaFaR: 19 Feb, 2008 - 06:52 AM
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Factorial Recursion
19 Feb, 2008 - 09:19 AM
Post #3

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
I would also improve the factorial function to something like this:

CODE

    public int fact(int N) {        
        if(N>1)            
            return fact(N-1)*N;        
        else
            return 1;
    }

User is offlineProfile CardPM
+Quote Post

AbuJaFaR
RE: Factorial Recursion
19 Feb, 2008 - 10:34 AM
Post #4

D.I.C Head
**

Joined: 13 Dec, 2007
Posts: 123


My Contributions
QUOTE(letthecolorsrumble @ 19 Feb, 2008 - 10:19 AM) *

I would also improve the factorial function to something like this:

CODE

    public int fact(int N) {        
        if(N>1)            
            return fact(N-1)*N;        
        else
            return 1;
    }



Nice smile.gif icon_up.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:39PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month