Thread: factorials in recursive function

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    11

    Question factorials in recursive function

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    void factorial(int);
    int num;
    int f;
    void main ()
    {
    	
    	cout<<"enter number";
    	cin>>num;
    
    	factorial (5);
    
    }
        void factorial (int num)
    
    {
    		if(num!=1)
    		{
    			cout<<"factorial is:"<<f<<endl;
    			exit(0)
    	
    		else
    		{
    
    f+f*num;
    num--;
    	
    	}
    
    factorial (num)
    
    	}
    --------------------------------------------------------------------------

    help me out with this, i keep getting it wrong


    Code tags added by kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already. Any further questions or ways I can help please feel free to PM me.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.


    Secondly, could you be more discriptive on what you're getting wrong? What is it doing? Is it compiling?


    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    f+f*num;
    num--;

    Perhaps you want to change that to:

    f = f*num;
    num--;

    Otherwise, your results won't be stored.
    Another thing. Perhaps you should try to restructure your factorial function. It is bad to be dependent on global variables, and you shouldn't exit the program from within it.
    Code:
    int Factorial(int Num)
    {
       if(Num <= 1)
       {
          return 1;
       }
       else
       {
          return Factorial(Num - 1);
       }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: factorials in recursive function

    Originally posted by CBeginner
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    void factorial(int);
    int num;
    int f;
    void main ()
    {
    	
    	cout<<"enter number";
    	cin>>num;
    
    	factorial (5);
    
    }
        void factorial (int num)
    
    {
    		if(num!=1)
    		{
    			cout<<"factorial is:"<<f<<endl;
    			exit(0)
    	
    		else
    		{
    
    f+f*num;
    num--;
    	
    	}
    
    factorial (num)
    
    	}
    --------------------------------------------------------------------------

    help me out with this, i keep getting it wrong


    Code tags added by kermi3
    your code does not have matched { }, fix this first
    hello, internet!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Re: factorials in recursive function

    Originally posted by moi
    your code does not have matched { }, fix this first
    and at the same time, loose this:
    >>void main ()
    and replace it with
    >>int main()
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  2. recursive function
    By tonderai76 in forum C++ Programming
    Replies: 11
    Last Post: 04-21-2004, 12:49 PM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM