CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2010
    Location
    Umeå, Sweden
    Posts
    2

    Compute a factorial.

    Heeeello!

    I'm trying to find a way how to compute a factorial of any given number.

    the factorial of a positive integer is defined as follows:

    n! = n(n-1)(n-2)(n-3).....

    what I'm trying to do here is to find a way so that the program will know how many steps it need to do to get to (1).

    example) I want to compute the factorial of the number 6.

    6! = 6(5)(4)(3)(2)(1) = 720

    I was just trying something like:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
            int n = 0;
    	int n1;
    	cout << "enter a positive integer and the compute the factorial of: ";
    	cin >> n;
    	n1 = n(n-1)(n-2)(n-3);
    	cout << n1;
    	_getch();
    	return 0;
    }
    ofc, this will not work but.. yeah, a little help please ^^

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Compute a factorial.

    Consider using a loop.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: Compute a factorial.

    A good place to start would be to have a look at "recursion" for factorial computation.
    your humble savant

  4. #4
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: Compute a factorial.

    Code:
    int factorial (int n)
    {	
    	int fact = 1;
    	for (int i=1; i<=n;i++)
    		fact = fact * i;
    	return fact;
    }
    Here is a simple function for calculating factorial, if there is something that you don't understand just ask.
    Last edited by Zlatomir; May 10th, 2010 at 11:07 AM.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Compute a factorial.

    Be aware, factorials grow fast. 12! will be the largest integer factorial which you can represent in a 32-bit int. Even a 64-bit int can only handle up to 20!. You'll need a BigInt library to handle anything larger.

  6. #6
    Join Date
    May 2010
    Location
    Umeå, Sweden
    Posts
    2

    Re: Compute a factorial.

    Quote Originally Posted by Zlatomir View Post
    Code:
    int factorial (int n)
    {	
    	int fact = 1;
    	for (int i=1; i<=n;i++)
    		fact = fact * i;
    	return fact;
    }
    Here is a simple function for calculating factorial, if there is something that you don't understand just ask.
    I tried this and it worked just fine.. but if u please, can you go through it step by step?

  7. #7
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: Compute a factorial.

    Code:
    int factorial (int n) // function that returns an int value and takes one int parameter
    {	
    	int fact = 1; 
    	for (int i=1; i<=n;i++) {  // this is a for-loop, this execute the code for n times 
                                                   // it loops from 1 to n
    
    		fact = fact * i;         // this makes fact equal to fact*i for n times 
            }
    	return fact;                     // return value of fact
    }
    http://www.cplusplus.com/doc/tutorial/control/ Here you have a little tutorial (link is about controlling the execution of a C++ program, the for loop is explained), but i recommend to at least take that tutorial from the beginning.

    But it is highly recommended to learn C++ from a book, because in a book the author take you from one topic to another only after he explains the topics you need to understand the new stuff.

    Here you have a free book(can be downloaded in pdf or xps format): http://msdn.microsoft.com/en-us/beginner/cc305129.aspx

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured