Well, first of all, this is not a factorial computation. Second, there is no space between #include and <iostream>. Third, you don't indicate what errors you are getting, although you are declaring (incorrectly) factorial(int n) in your local variable declaration list. It is (probably), an external function, and should be declared before main(), as in:
#include <iostream>
using namespace std;
extern int factorial(int); // Unnecessary in the context of this code.
int main(void)
{
int num, n;
cout<<"Enter a number "<<endl;
cin>>num;
for(n=num; num>1 ;num--)
{
n *= (num-1);
cout << n << endl;
system("pause"); // Is this necessary?
}
printf("Factorial(%d) == %d\n", num, n);
}
rubberman
Posting Maven
2,581 posts since Mar 2010
Reputation Points: 365
Solved Threads: 308
Skill Endorsements: 52
Second, there is no space between #include and <iostream>.
So? What's the problem you're pointing out?
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
Ahhh, we see. You are not a serious student of programming. You are just a hobbyist and don't really need to learn and understand the proper terminology. You only want to program for fun. That's fine. Just keep in mind many of us are professionals, and most are striving to be professionals, so if we correct you, it's not you. It's just our need to be accurate.
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
For something like factorial operations that typically operate on integer values, whether you want to use double precision floating point values depends a lot on what the largest result value is likely to be. If you use 64-bit integers (long long int), then this is not such a problem, but before compilers supported 64-bit integers, where even a long integer would be a 32-bit values, this was often necessary, as a 32-bit signed integer can only represent a range of +-2147483647, or 4,294,967,295 if you are using unsigned integers. So, the factorial of a fairly large number can easily overflow a 32-bit value. An unsigned 64-bit unsigned integer can be up to 18446744073709551615 - or 19+ digits, which is, for integer values, preferable to double precision (64-bit) numbers which are typically limited to about 15 digits of accuracy, and they suffer from intermediate rounding errors that integers will not.
Note that 20! == 2432902008176640000 will almost overflow a 64-bit integer, and cannot be properly represented with a double. New compilers will support long doubles (128 bits), so the march to ever bigger numbers continues... :-)
rubberman
Posting Maven
2,581 posts since Mar 2010
Reputation Points: 365
Solved Threads: 308
Skill Endorsements: 52
Also, if you are interested, 50! == 30414093201713378043612608166064768844377641568960512000000000000
and 100! == 9.332621544e+157, or 9,332,621,544 with 148 extra zeros tacked onto the end.
rubberman
Posting Maven
2,581 posts since Mar 2010
Reputation Points: 365
Solved Threads: 308
Skill Endorsements: 52