|
-
May 10th, 2010, 10:51 AM
#1
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 ^^
-
May 10th, 2010, 10:58 AM
#2
-
May 10th, 2010, 10:58 AM
#3
Re: Compute a factorial.
A good place to start would be to have a look at "recursion" for factorial computation.
your humble savant
-
May 10th, 2010, 11:02 AM
#4
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.
-
May 10th, 2010, 11:04 AM
#5
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.
-
May 10th, 2010, 11:24 AM
#6
Re: Compute a factorial.
 Originally Posted by Zlatomir
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?
-
May 10th, 2010, 02:27 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|